Draw line given angle and one co-ord

Anyone know how i could draw a line given just one co-ord and an angle e.g. angle of 90 would be vertical, 0 would be horizontal?Any help would be great!ThanksD.
[189 byte] By [dave_dina] at [2007-9-29 22:40:45]
# 1
g.drawLine((int)x,(int)y,(int)(x+Math.sin(angleInRadians)*LENGTH),(int)(y-Math.cos(angleInRadians)*LENGTH));
Abusea at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 2

Thanks a million, its workin pretty well but may need tweaking. One thing tho, at 0 radians its drawing a vertical line. I thought it would be horizontal at 0, 180 etc.

Basically the user can input desired angle of a shot. I want this to be vertical at 90, horizontal at 0, and have everything in between. Will i need a conversion to degrees, or something like that?

Thanks again, much appreciated.

D.

dave_dina at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 3
add Math.PI/2 if you're off by 90 degrees.
Malohkana at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 4
dam the radians! dam them to hell
penguins404a at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 5
if you want horizontal to be 0, swap the sin and cos. (you may also have to fiddle with the signs)
Abusea at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 6
Math.toDegrees();use that guy if you really don't wanna remember your trig ;)
Malohkana at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 7
ever the handy Maths class!cheers guys!D.
dave_dina at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 8

One more quick question.

When the angle in degrees passes 90, say its 135 i want it pointing north west, i convert this to radians, and then pass it into the relevant cos and sin functions, but its animating it north east... is this down to the radians or am i just missing something? probably the latter.. ;)

Thanks Again,

D.

dave_dina at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 9

This works:

g.drawLine(x,y,

(int)(x+Math.cos(degrees/180.0*Math.PI)*length),

(int)(y-Math.sin(degrees/180.0*Math.PI)*length));

/180*Math.PI might be replaced with a call to Math.toRadians() if you dont want to support 1.1

Ragnvald_id2a at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 10
I don't understand many ppls aversion to Radians,I find using them makes far more sense, and the code far neater (not to mention faster ^_^)Each to their own I guess.
Abusea at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 11

Plus, you can be way more accurate with radians.

I understand what you're saying. I'm a sucker for measuring angles in degrees then converting, infact all my rotation methods take the angle in degrees then convert to radians(: S). In my case I use them just because (at the moment) its easier to visualise, but I am trying to convert. It's all new to me this maths lark :)

From now on a circle is not 360 degrees its 2*Math.PI.

KarateMarca at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 12

It may have to do in part with the educational system here (USA). Every protractor I have ever seen in any school uses degrees to measure the angles, and geometry is first taught from the perspective of degrees rather than radians, so it is possible that the preference is built-in that way. That, and dealing with whole numbers always seems simpler than fractions, especially when you're talking about computer programming and the limitations of representing such numbers in the computer.

I guess it's all in what you're used to.

-Dok

Dr-Matrixa at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...
# 13
1 advantage of radians, it prevents you from ever hard coding values in :)values are always in relation to PI, so you don't get nasty things like x+90 (x+Math.PI/2), or x+180 (x+Math.PI) sneeking into your code :D
Abusea at 2007-7-16 3:03:24 > top of Java-index,Other Topics,Java Game Development...