Re: The Euphoria Sub Commander project

new topic     » goto parent     » topic index » view thread      » older message » newer message

don cole wrote:
> 
> 
> Could someone please explain the following code to me in detail? I'm
> especially
> confused by the
> {} brackets.
> 
> }}}
<eucode>
> 
> k=player --1 or 2
> cen={{120,360},{520,360}},     -- centers of clock

This is a sequence (outer pair of braces) containing two pairs of integers (the
two inner pairs of braces). Each pair is the {x,y} cartesian coordinates of a
point on the screen.

> r=100,     -- radius of clock
> rt=remaining time in secs

This last line confuses me, something must be missing.

> 
> 
> Don Cole
>  
> function p2xy(atom r, atom a)   -- polar to xy
>    return {-r*sin(a),-r*cos(a)}
> end function

Basic trigonometry to go from polar (radius,angle) coords to cartesian (x,y)
coords. The returned value is negative, because the author wished to
write "+p2xy()" rather than "-p2xy()".

> 
> draw_line(colors[k],{cen[[k],cen[k]+p2xy(r,rt/1800*PI)})
> 

cen[[k] must be cen[k], or else the code simply wasn't tested at all.

draw_line() takes an integer (a color) and a sequence of points. A point is
given by its coordinates expressed as {x,y}. This builtin, DOS specific procedure
joins the points in the order given, with a line of unspecified thickness and
supplied color. You can look it up in the docs for Euphoria.

p2xy() returns a pair of offsets, and cen[k] is a pair of coordinates.
cen[k]+p2xy() is the point cen[k] translated by the offset vector p2xy() returns.
Hence both cen[k] and cen[k]+p2xy() are pairs of coordinates, which can appear
in a sequence passed to draw_line().

sin() and cos() expect an argument expressed in radians. A radian is a
measurement unit for angles. A right angle is PI/2 radian long, so a full circle
is 2*PI radian long (PI is defined in misc.e). As a result, PI radians are 180
degrees, so the conversion formula from degrees to radians is rad = deg/180*PI.
So I assume rt is a number of tenths of degree, very likely. Examine the code
further for confirmation of this.

Contrary to standard mathematical conventions, * and / have the same precedence
in Euphoria, so that "x/y*z" is (x/y)*z, not x/(y*z). This is sometimes a problem
when porting math formulas to Euphoria.

> </eucode>
{{{

> 
> 
> This code comes from       : Simple Chess Clock / Countdown Timer
>                              Author     : Ernest Cheam  
>                              
>                              
> In the Archives.
> 
> Thank you,

I wasn't sure what was confusing you in the code; I hope the above has made
things clearer.

CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu