Line2d implements GeometricObject, Cloneable { public Point2d reflectAroundLine( Point2d pointToReflect ) { double nx = end.getX() - begin.getX(); double ny = end.getY() - begin.getY(); double d = Math.sqrt(nx*nx + ny*ny); nx /= d; ny /= d; double px = pointToReflect.getX() - begin.getX(); double py = pointToReflect.getY() - begin.getY(); double w = nx*px + ny*py; double rx = 2*begin.getX() - pointToReflect.getX() + 2*w*nx; double ry = 2*begin.getY() - pointToReflect.getY() + 2*w*ny; return new Point2dImpl( (float)rx, (float)ry ); } Line2d();  Line2d(Point2d begin, Point2d end);  Line2d(float x1, float y1, float x2, float y2); void setBeginPoint(Point2d begin); void setEndPoint(Point2d end); Point2d getBeginPoint(); Point2d getEndPoint(); Point2d setEndPoint(); IntersectionResult getIntersection( Line2d otherLine ); Point2d reflectAroundLine( Point2d pointToReflect ); Line2d lineWithinSquare(Rectangle r); static double distance(Point2d p1, Point2d p2); static double distance(float p1x, float p1y, float p2x, float p2y); static Line2d lineFromRotation(int x1, int y1, double theta, int length); double calculateLength(); double calculateHorizontalAngle(); double calculateVerticalAngle(); @Override Line2d transform(Matrix transform); Line2d getNormal(); Line2d getNormal( Point2d p ); @Override void translate( float x, float y ); boolean isOnLine( Point2d p, float tolerance ); boolean isInLine( Point2d p, float tolerance ); @Override String toString(); @Override Rectangle calculateRegularBoundingBox(); @Override void scale(float sc); @Override void scale(Point2d centre, float sc); @Override void scaleCOG(float sc); @Override Point2d getCOG(); @Override double minX(); @Override double minY(); @Override double maxX(); @Override double maxY(); @Override double getWidth(); @Override double getHeight(); Point2dImpl toUnitVector(); @Override Line2d clone(); public Point2d begin; public Point2d end; }
@Test public void testReflection() { Line2d line = new Line2d( new Point2dImpl(0,0), new Point2dImpl(8,4) ); Point2d reflection = line.reflectAroundLine( new Point2dImpl( 1,2 ) ); assertEquals(2.2, reflection.getX(), 0.001); assertEquals(-0.4, reflection.getY(), 0.001); }