Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Drawing Lines by Angles

Matthew Sparkes
Greenhorn
Posts: 2
Hi everyone, this is my first post here.

I'm writing a program at the moment, where I will have an array of ints representing angles. I will then have a series of dots connected by lines of fixed length, where the dots deviate from the upright by a certain angle.

This will allow me to plot a graph, but only storing one set of numbers.

Currently I have worked out the positions manually, and these are kept in X and Y int arrays. What I want is to be able to do something like I am currently doing with polyline, but instead using angles rather than cartesian points, but all the while keeping the seperation (line length) constant.

Any ideas?

Keith Lynn
Ranch Hand
Posts: 2409
What about using the angles to generate the Cartesian points?

Matthew Sparkes
Greenhorn
Posts: 2
Yeah, that would be fine, but my math is a little rusty.

What sort of code would be needed if I had x,y as starts points, l as length of line, and a as angle?

I could then take this, and run through th array, adding the coordinates next to the angle, adding the coordinate of point 2 to that of point 1 and so on.

Keith Lynn
Ranch Hand
Posts: 2409
Well remember that in a right triangle, if the hypotenuse is of length l, and one of the acute angles is a, then the length of the side adjacent is l*cos(a), and the length of the side opposite it is l*sin(a). Remember that in Java, Math.sin and Math.cos expect radian values and there are pi radians in a semicircle.