1

I have this method to draw a triangle:

public void draw(Graphics g, int targetX, int targetY){

    /*CODE TO ADJUST X AND Y*/

    g.setColor(Color.white);
    g.fillPolygon(new int[]{x - 4, x - 4, x + 9}, new int[]{y - 4, y + 4, y}, 3);
}

x and y are instance variables that reflect the centre point of the triangle and the triangle is drawn around this point. It's an isosceles triangle so that it's 'pointing' at something. With the parameters I use there it's pointing directly to the right.

targetX and targetY are the point that the triangle is moving towards. With each repaint x and y are incremented or decremented to move them towards the targets.

So what I'd like to do is draw the triangle so that it's pointing towards the target co-ordinates. With the approach I have right now I guess I would need to write a function that fills in the 'x - 4' 'x - 9', etc... calculations in the fillPolygon parameter with values that set the triangle in the right direction but I'm not really the best maths guy and so I have no idea what calculations such a function might need to contain.

I'm also pretty new to Swing so I have no idea whether my approach to drawing these triangles and moving them towards a certain point is even a good one so if anybody has any suggestions then I am all ears!

Any help with this would be much appreciated.

AdamLazaruso
  • 562
  • 2
  • 6
  • 13
  • Sounds like more of a question for math.stackexchange.com than a Java question. – David Conrad Feb 27 '14 at 18:32
  • Maybe, but perhaps there are existing Java functions that might help to achieve this? Also like I say I'm not entirely sure that my approach here is a good one to begin with. I don't think this is a pure maths question. – AdamLazaruso Feb 27 '14 at 18:34
  • There is a nice class that offers Affine transformations (http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html). If you define your AT as rotation with your desired angle (which you computer with sin/cos), this should work fine. – exception1 Feb 27 '14 at 18:38

2 Answers2

1

You can use the java Math package's trig functions to get the angle from the triangle's center and the destination point and use that to calculate the three points of the triangle based on the desired width and height. This might be tough if you don't know basic trigonometry though.

Someguy
  • 26
  • 1
1

As I indicated in the comments, the AffineTransform (http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html) is everything that you need.

To create your AT, call:

AffineTransform.getRotateInstance(double vecx, double vecy, double anchorx, double anchory)

where anchor is your rotation center (your x and y) and vecx|vecy is your rotation vector. In your case, the rotation vector is targetx|targety. The rotation vector is the vector which, if you rotate the initial x-axis, will lie on the result of the rotation. As the top of your triangle lays on the x-axis, we can use it without any further computation.

Then, you can transform your points:

Point2D p1 = new Point2D(x - 4, y - 4);
Point2D rotatedP1 = new Point2D(0, 0);
at.transform(p1, rotatedP1);

This will return you the rotated point. Continue with your two other points. The triangle should now point to you defined target. If you store your points in an array, you can rotate them with one method call: See here.

Hope it helps.

exception1
  • 1,239
  • 8
  • 17