1. submarine commander project
- Posted by don cole <doncole at pacb?l?.net> Dec 09, 2007
- 535 views
- Last edited Dec 10, 2007
Can any of you geometry people out there help me?
function p2xy(atom r, atom a) -- polar to xy return {-r*sin(a),-r*cos(a)} end function
all measurements in pixels. r=distance from center x=clock in degrees 3600 = center to top center=vector {400,273 } fin=cen+p2xy(r,x/1800*PI) --the outmost position of the radius drawLine(Pix,cen[1],cen[2],floor(fin[1]),floor(fin[2])) --one line of the sweep. This formula gives me the position of the final dot of the radius. Works good. What I would like to do is the opposite to find the clock degrees and last radius position given the center and the vector. For example let's say center={400,273}. A dot is out there at {210,120}. What would be the clock degrees to that dot? 3600=12 o'clock 1800=6 o'clock 2700=9 o'clock etc... And how many pixels from the center to that dot? Don Cole
2. Re: submarine commander project
- Posted by David Money <dmoney290 at ao?.c?m> Dec 10, 2007
- 509 views
don cole wrote: > > Can any of you geometry people out there help me? > > }}} <eucode> > function p2xy(atom r, atom a) -- polar to xy > return {-r*sin(a),-r*cos(a)} > end function > </eucode> {{{ > all measurements in pixels. > > r=distance from center > x=clock in degrees 3600 = center to top > center=vector {400,273 } > fin=cen+p2xy(r,x/1800*PI) --the outmost position of the radius > drawLine(Pix,cen[1],cen[2],floor(fin[1]),floor(fin[2])) --one line of the > sweep. > > > This formula gives me the position of the final dot of the radius. Works good. > > What I would like to do is the opposite to find the clock degrees and last > radius position > given the center and the vector. > > For example let's say center={400,273}. > > A dot is out there at {210,120}. > > What would be the clock degrees to that dot? > 3600=12 o'clock > 1800=6 o'clock > 2700=9 o'clock etc... > And how many pixels from the center to that dot? > > Don Cole I would include the conversions in the functions, so you could use your 0~3600 angle directly. Something like this: <code> constant PI=3.1415926535893238 --or include misc.e function p2xy(atom r,atom sec) --r is distance, sec is angle (seconds after hour, measured from 12 o'clock) atom a a=sec*PI/1800 --convert to radians return {r*sin(a),-r*cos(a)} end function function vector(sequence pt1,sequence pt2) --distance and angle from pt1 to pt2 integer dx,dy atom r,a dx=floor(pt2[1]-pt1[1]) dy=floor(pt2[2]-pt1[2]) r=sqrt(dx*dx+dy*dy) if r=0 then return {0,0} end if if dy<0 then if dx<0 then a=2*PI-arctan(dx/dy) else a=-arctan(dx/dy) end if elsif dy>0 then a=PI-arctan(dx/dy) else --dy=0 (one point above the other); can't use arctan formula if dx<0 then a=3*PI/2 else a=PI/2 end if end if return {r,1800*a/PI} --convert radians to decidegrees end function </code> The arctan function just works for -pi/2 to pi/2, so you have to figure out which quadrant you are in, and offset it. Also, you have to avoid dividing by zero. If r=0, the points are the same; the function will return 0 for the angle, too, but it's really undefined. You had two minus signs in your function--was that a typo? That would work for conventional angle measurements (counterclockwise), but it would make your clock run backwards. --David
3. Re: submarine commander project
- Posted by don cole <doncole at pacbell??et> Dec 10, 2007
- 521 views
David Money wrote: [snip] > > I would include the conversions in the functions, so you could use your > 0~3600 angle directly. Something like this: > > <code> > constant PI=3.1415926535893238 --or include misc.e > > function p2xy(atom r,atom sec) > --r is distance, sec is angle (seconds after hour, measured from 12 o'clock) > atom a > a=sec*PI/1800 --convert to radians > return {r*sin(a),-r*cos(a)} > end function > > function vector(sequence pt1,sequence pt2) > --distance and angle from pt1 to pt2 > integer dx,dy > atom r,a > dx=floor(pt2[1]-pt1[1]) > dy=floor(pt2[2]-pt1[2]) > r=sqrt(dx*dx+dy*dy) > if r=0 then > return {0,0} > end if > if dy<0 then > if dx<0 then > a=2*PI-arctan(dx/dy) > else > a=-arctan(dx/dy) > end if > elsif dy>0 then > a=PI-arctan(dx/dy) > else --dy=0 (one point above the other); can't use arctan formula > if dx<0 then > a=3*PI/2 > else > a=PI/2 > end if > end if > return {r,1800*a/PI} --convert radians to decidegrees > end function > </code> > > The arctan function just works for -pi/2 to pi/2, so you have to figure > out which quadrant you are in, and offset it. Also, you have to avoid > dividing by zero. If r=0, the points are the same; the function will return > 0 for the angle, too, but it's really undefined. > You had two minus signs in your function--was that a typo? That would work > for conventional angle measurements (counterclockwise), but it would make > your clock run backwards. > --David Thank you David for responding, That sould about do it. I'll try it soon. That was not a typo I got it from chessclock in the archives. I corrected the problem with: for x=3600 to 1 by -1 do Don Cole
4. Re: submarine commander project
- Posted by ChrisBurch2 <crylex at f?eeuk.co?uk> Dec 10, 2007
- 509 views
Hi Don Looking good. I found this quite a good site for basic trig - its for flash, but easy enough to translate http://www.peachpit.com/articles/article.aspx?p=30617&rl=1 Also this one, goes into a little 3d http://www.kirupa.com/developer/actionscript/3dindex.htm Chris
5. Re: submarine commander project
- Posted by don cole <doncole at pac?ell.n?t> Dec 11, 2007
- 503 views
ChrisBurch2 wrote: > > Hi Don > > Looking good. > > I found this quite a good site for basic trig - its for flash, but easy > enough to translate > > <a > href="http://www.peachpit.com/articles/article.aspx?p=30617&rl=1">http://www.peachpit.com/articles/article.aspx?p=30617&rl=1</a> > > Also this one, goes into a little 3d > > <a > href="http://www.kirupa.com/developer/actionscript/3dindex.htm">http://www.kirupa.com/developer/actionscript/3dindex.htm</a> > > Chris Hello Chris, I'm about done with the sonar part and hope to be posting it soon. I was using sequences and saving the whole scope. Then looking the vectors up. I think this new way is faster so I have to revamp some things. Don Cole