0

I am currently working on a project in which i need to draw a non-right triangle in the center of a JFrame using either, java.awt.Graphics's drawLine() or drawPolygon() methods. Both of these methods require the coordinates of all of the points to function. My problem is that instead of points, all i have are all of the angles and side lengths of the triangle. I've drawn up a nifty diagram of what I hope helps you visualize my problem:

enter image description here

(EDIT the position of C in this Senario is not fixed betwen a and b and may be anywhere below the axis on which AB rests)

as you can see from my picture, I need the coordantes of C based off the coordanes of A, is there any way to calculate this given the lengths of all sides and angles of the non-right triangle?

Bonus: How would i find an (x, y) value for A that would effectivly center the triangle in the middle of the JFrame?

James Oswald
  • 510
  • 3
  • 12
  • 25
  • 1
    Use trigonometry. Its an almost trivial problem. If you haven't studied it, you really really need to for 2d drawing. – Gabe Sechan Dec 01 '16 at 01:04
  • Given the coordinates of `A`, the coordinates of `C` can be entirely determined by the length `b` and the angle at `A` — it is a simple line equation; the fact that it's a part of a triangle isn't even relevant until you try to determine the center. I would start all calculations with `A` at coordinates `0,0` then _translate_ the result. Center of a triangle: http://www.dummies.com/education/math/trigonometry/how-to-pinpoint-the-center-of-a-triangle/ – Stephen P Dec 01 '16 at 01:22
  • I really wish someone would just post the answer instead of just commenting that the answer is trigonometry... – James Oswald Dec 01 '16 at 01:24
  • You might get better results on the math site, since this really has nothing to do with programming. – Tim Biegeleisen Dec 01 '16 at 01:24
  • @TimBiegeleisen i can't post images on the math site until i get over 15 reputation on it unfortunately. – James Oswald Dec 01 '16 at 01:25
  • @StephenP So are you saying you would _translate_ any triangle to the position shown and then just use basic trigonometry to find the coordinates of `C`? – Tim Biegeleisen Dec 01 '16 at 01:31
  • @Tim I'm saying I would do all calculations based on coordinate `A` being at `(0,0)` — finding the coordinates of `B` and `C` and the center of the triangle, and then do a _"translation"_ to put the center of the triangle in the center of the JFrame. That translation provides a matrix that you use to translate the individual A, B, and C points to their ultimate destination coordinates. – Stephen P Dec 01 '16 at 01:45

1 Answers1

1

If you know angle CAB, the coordinate of point C should be:

(x+b·sin(θ), y-b·cos(θ))

In Java, there is:

double Math.sin(double radians);
double Math.cos(double radians);

Keep in mind that the angle needs to be in radians. If your angles are in degrees, try:

double Math.sin(Math.toRadians(double degrees));
double Math.cos(Math.toRadians(double degrees));

Hope this helps.

  • Thank you! i recommend revising your answer with a photo representing what Theta is in both of those contexts once you get high enough reputation to do so. – James Oswald Dec 01 '16 at 05:06