Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

angle to rectangle perimeter position #1

Open
r043v opened this issue Oct 4, 2013 · 1 comment
Open

angle to rectangle perimeter position #1

r043v opened this issue Oct 4, 2013 · 1 comment

Comments

@r043v
Copy link

r043v commented Oct 4, 2013

hi dude, pretty nice tool !

i was need to write a similar code as your one for converting angle to rectangular perimeter coordinate,

basicaly the same way but maybe use a bit less cpu due to there is no need for a /
(you still need divide but depending stuff can be precomputed, based on canvas size)

a,b,c are the 3 first corner points, p is perimeter, rp the ratio between p and 360, pp is the position on the perimeter

/* convert angle to rectangle perimeter coordinates */
function angle2rect(angle,sx,sy){
    while(angle < 0) angle += 360; angle %= 360;

    var a = sy, b = a+sx, c = b+sy, p = (sx+sy)*2, rp = p/360;
    var pp = parseInt( ( (angle*rp)+(sy/2) )%p ,10);

    if(pp <= a) return { x:0,  y:sy - pp };
    if(pp <= b) return { y:0,  x:pp - a  };
    if(pp <= c) return { x:sx, y:pp - b  };
                return { y:sy, x:sx - ( pp - c ) };
}

http://jsfiddle.net/r043v/Z7Zg7/

@evictor
Copy link
Owner

evictor commented Oct 5, 2013

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants