0

So I am trying to draw an arrowhead (anywhere really) along a Quad2D curve that specifies a curve between two different points. I want the arrowhead to point towards the final point but cant figure out how to do this. I am familiar with using a Java polygon to create the arrowhead and I tried rotating it using an affine transform (after trying to find the angle using the answer to this question here Calculating angle between two points - java).

My geometry is quite weak and any other suggestions in regards to drawing the curve which would make it easier to position the arrowhead would also be appreciated.

        QuadCurve2D.Double my_arc = new QuadCurve2D.Double(x1, y1, cx, cy, x2, y2);
        QuadCurve2D.Double part1 = new QuadCurve2D.Double();
        QuadCurve2D.Double part2 = new QuadCurve2D.Double();
        my_arc.subdivide(part1, part2);

Is how I created the arc, the subdivision is used to find the rough center point of the arc as ideally I want to have the arrowhead in the center (but at the end is fine too) EDIT:

        double deltaY = ARC1.y2 - ARC.y1;
        double deltaX = ARC1.x2 - ARC.x1;
        double angle = Math.atan2(deltaY, deltaX);

        Line2D l = new Line2D.Double((int)ARC1.x2, (int)ARC1.y2, (int)ARC.x1, (int)ARC.y1);

        int px = (int)ARC1.getX2() - 5;
        int py = (int)ARC1.getY2();         

        Polygon pp = new Polygon(new int[] {px, px+5, px+10},new int[] {py, py+15, py}, 3 );

        AffineTransform tt = new AffineTransform();
        tt.rotate(angle, pp.getBounds2D().getCenterX(), pp.getBounds2D().getCenterY());

        g.draw(tt.createTransformedShape(pp));
        g.setColor(Color.red);
        g.draw(l);
        g.setColor(Color.pink);
        g.draw(pp);

Using the above code I get this result Image of results Where the red triangle is the rotated one, am I missing something, like i said my geometry is a bit off

Community
  • 1
  • 1
  • Basic answer is to create the arrow shape and then transform it to point in the direction you want, for [example](http://stackoverflow.com/questions/15607427/java-make-a-directed-line-and-make-it-move/15607737#15607737), [example](http://stackoverflow.com/questions/20129281/mouse-motion-listener-only-in-one-direction/20131064#20131064) and [example](http://stackoverflow.com/questions/30991742/how-to-draw-2d-arrows-with-java-swing/30992415#30992415) – MadProgrammer Oct 18 '15 at 04:11
  • I am familiar with using the affine transforms but the issue is I cant actually get the angle of rotation correct, or more accurately I dont actually know how to properly get the angle... – user3466639 Oct 18 '15 at 04:18
  • Well, I assume you know your current point and your target point, so you just need to calculate the angle between those two points – MadProgrammer Oct 18 '15 at 04:39

0 Answers0