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