Jump to content


Photo

Distance From Point To Edge Of Circle?


  • Please log in to reply
12 replies to this topic

#1 Binsk

Binsk

    -Insane Kid-

  • GMC Member
  • 3111 posts
  • Version:GM:Studio

Posted 03 April 2013 - 04:35 PM

Hello, I have a small issue and my brain just isn't working today. I have a circle, defined by an x/y position and radius. I also have a point facing in a direction, defined by an x/y position and direction. The point will ALWAYS be inside of the circle, and I need to determine the distance from the point to the edge of the circle in its given direction. 

 

How can I go about calculating this? I know there is some trig in there, but, as said, my brain just isn't working well today. -.-"

 

Any help is appreciated, thank you.


  • 0

NEWS FLASH! You can't get to heaven by your works. No chance. Hopeless. Give up now. No amount of works will get you anywhere, in fact all your efforts have been botched years ago because you already missed perfection the first time you lied, stole, or performed any of those other lovelies.

 

That said, do you want to go to heaven? Not the hellish picture given by cartoons. You know, the nightmarish one where you are renting a cloud and harp? I mean, holy crap, where did that come from? I am talking about a bonafide city, people, designed by God no less. Heck, read John's Revelation 21 if you want details. Getting to heaven is extremely simple, actually, the how and why is spelled out in Paul's letter to the Romans, summarized in Romans 10:9,10. God lived a perfect life and died in your place, covering every one of your mistakes and failures past and future. All you have to do is believe that He did what he said He did and accept the gift. Bam, you're saved. Done deal. Complicated, no? Questions? PM me.


#2 Chrscool8

Chrscool8

    Call me Chris.

  • GMC Member
  • 2399 posts
  • Version:GM:Studio

Posted 03 April 2013 - 04:56 PM

I don't know if I'm really understanding your plan here, but off the top of my head, I'll propose a rudimentary solution...

 

What if you just iteratively walked from the point until the point_distance between the walked position and circle x/y is more than the circle's radius?


Edited by chrscool8, 03 April 2013 - 04:57 PM.

  • 0

Click the banner to check out my game, Suh Burb!

XPTintI.gif

Add me on Steam! Let's talk about making games!


#3 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 13885 posts
  • Version:GM:Studio

Posted 03 April 2013 - 05:06 PM

distance from edge = circle_radius - point_distance(circle_x, circle_y, object.x, object.y)

 

If the result is positive, the object is inside the circle.


Edited by TheouAegis, 03 April 2013 - 05:07 PM.

  • 1

"The problem with object-oriented languages is that they've got all this implicit environment that they carry around with them. You wanted a banana but what you've got is a gorilla holding the banana and the entire jungle." -Joe Armstrong

 

"It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical?" -Alan Perlis

 

"Do not think about where you want to be and how to get there; think about what you want to do and how to do it." -Theou Aegis


#4 Binsk

Binsk

    -Insane Kid-

  • GMC Member
  • 3111 posts
  • Version:GM:Studio

Posted 03 April 2013 - 05:48 PM

@Chrs - Although that would work, I cannot afford to do that because this calculation has the possibility of taking place with 100's of points per step. Thank you for the idea.

 

@Theou, Thanks for the feedback. However, I already know the point is inside the circle. That is a fact. The problem is the point can have ANY direction assigned to it, and I need to know the distance from the point IN THE DIRECTION ASSIGNED to the edge of the circle. I am not really sure how to do this efficiently. 


  • 0

NEWS FLASH! You can't get to heaven by your works. No chance. Hopeless. Give up now. No amount of works will get you anywhere, in fact all your efforts have been botched years ago because you already missed perfection the first time you lied, stole, or performed any of those other lovelies.

 

That said, do you want to go to heaven? Not the hellish picture given by cartoons. You know, the nightmarish one where you are renting a cloud and harp? I mean, holy crap, where did that come from? I am talking about a bonafide city, people, designed by God no less. Heck, read John's Revelation 21 if you want details. Getting to heaven is extremely simple, actually, the how and why is spelled out in Paul's letter to the Romans, summarized in Romans 10:9,10. God lived a perfect life and died in your place, covering every one of your mistakes and failures past and future. All you have to do is believe that He did what he said He did and accept the gift. Bam, you're saved. Done deal. Complicated, no? Questions? PM me.


#5 enp

enp

    GMC Member

  • GMC Member
  • 132 posts
  • Version:GM8

Posted 03 April 2013 - 06:02 PM

Hi, I wrote this script by myself, copy and paste it in a script.

 

x -> x coordinate of your point

y -> y coordinate of your point

dir -> direction on which you have to calculate the direction
xc -> x coordinate of the center of your circle

yc -> x coordinate of the center of your circle

radius -> radius of your circle

 

returns the distance from the point and the collision point on the circumference or -1 if the point is outside the circle

//distance_to_circumference(x,y,dir,xc,yc,radius)

if point_distance(argument0,argument1,argument3,argument4) <= argument2 return sqrt(sqr(argument5)-sqr(cos(degtorad((90 - ((argument2 - point_direction(argument0,argument1,argument3,argument4) + 360) mod 360))))*point_distance(argument0,argument1,argument3,argument4)))+sin(degtorad((90 - ((argument2 - point_direction(argument0,argument1,argument3,argument4) + 360) mod 360))))*point_distance(argument0,argument1,argument3,argument4)
return -1

// (C) by Nicola Paro (ENP)
// This is a free code and you can use as you wish
// If you use this on a commerciail game, please give credit :) THX

 

 

A simple code you can put on a draw event to test the code

if not variable_local_exists('dir') dir = 0
Px = mouse_x
Py = mouse_y
Pangle = dir
if mouse_wheel_up() dir+= 3
if mouse_wheel_down() dir-= 3

Cx = room_width/2 
Cy = room_height/2
Cr = 64

distance = distance_to_circumference(Px,Py,Pangle,Cx,Cy,Cr)


Collisionx = Px+lengthdir_x(distance,Pangle)
Collisiony = Py+lengthdir_y(distance,Pangle)


draw_circle(Cx,Cy,Cr,true)
draw_line(Px,Py,Px+lengthdir_x(320,dir),Py+lengthdir_y(320,dir))
draw_circle(Collisionx,Collisiony,4,true)

I hope i helped you :)

EDIT: solved a little bug in the formula ;)


Edited by enp, 03 April 2013 - 06:18 PM.

  • 0

#6 Chrscool8

Chrscool8

    Call me Chris.

  • GMC Member
  • 2399 posts
  • Version:GM:Studio

Posted 03 April 2013 - 06:54 PM

@Chrs - Although that would work, I cannot afford to do that because this calculation has the possibility of taking place with 100's of points per step. Thank you for the idea.

 

Yeah, I figured. The only way to improve that would be like a binary search method. Anyways, looks like you've got something else here.

 

if point_distance(argument0,argument1,argument3,argument4) <= argument2 return sqrt(sqr(argument5)-sqr(cos(degtorad((90 - ((argument2 - point_direction(argument0,argument1,argument3,argument4) + 360) mod 360))))*point_distance(argument0,argument1,argument3,argument4)))+sin(degtorad((90 - ((argument2 - point_direction(argument0,argument1,argument3,argument4) + 360) mod 360))))*point_distance(argument0,argument1,argument3,argument4)
return -1

I have no idea if that works, but holy crap.


Edited by chrscool8, 03 April 2013 - 06:55 PM.

  • 0

Click the banner to check out my game, Suh Burb!

XPTintI.gif

Add me on Steam! Let's talk about making games!


#7 TheSnidr

TheSnidr

    Heavy metal viking dentist

  • GMC Elder
  • 3345 posts
  • Version:GM:Studio

Posted 03 April 2013 - 07:04 PM

/*
line_circle_intersection(cx, cy, r, px, py, vx, vy)
Finds the intersection between a line [px,py] + t * [vx,vy], where [vx,vy] is a unit vector, and a circle centered at (sx,sy) with radius r
Returns the distance from the point to the circle and the point of intersection (returnX, returnY)
Script made by TheSnidr
www.thesnidr.com
*/
var a, b, d, dp, xx, yy;
xx = argument0 - argument3
yy = argument1 - argument4
a = argument5
b = argument6
dp = xx * a + yy * b
d = sqr(dp) + sqr(argument2) - sqr(xx) - sqr(yy)
if d < 0 {return 0}
d = sqrt(d)
if dp < d   {dp += d if dp < 0{return 0}}
else        {dp -= d}
returnX = argument3 + a * dp
returnY = argument4 + b * dp
return dp;

Simplified a line_sphere intersection script to 2D. Haven't tested it after the conversion, but I think it should work

 

EDIT:
If you know the point is inside the circle, the script can be simplified to this:

 

/*
line_circle_intersection(cx, cy, r, px, py, vx, vy)
Finds the intersection between a line [px,py] + t * [vx,vy], where [vx,vy] is a unit vector, and a circle centered at (sx,sy) with radius r
Returns the distance from the point to the circle and the point of intersection (returnX, returnY)
Script made by TheSnidr
www.thesnidr.com
*/
var a, b, d, dp, xx, yy;
xx = argument0 - argument3
yy = argument1 - argument4
dp = xx * argument5 + yy * argument6
d = dp + sqrt(sqr(dp) + sqr(argument2) - sqr(xx) - sqr(yy))
returnX = argument3 + argument5 * d
returnY = argument4 + argument6 * d
return d;

  • 0

#8 mr magnus

mr magnus

    Viking King

  • GMC Member
  • 3739 posts
  • Version:GM:Studio

Posted 03 April 2013 - 07:30 PM

Here's one more; and this time just using basic trigonometry. :)

 

/*edge_distance(x,y,xx,yy,dir,Radius)
returns the distance from the point {xx,yy} to the edge of the circle with the centerpoint {x,y} and radius Radius in the direction dir
{x,y} = the centerpoint of the circle
{xx,yy} = the location of the point being tested
dir = the direction to head to from the point towards the edge
Radius = the radius of the circle.
*/
var _x, _y, _xx, _yy, _dir, _B, _A, _C, _angle; //initialize temp variables
_x=argument0;
_y=argument1;
_xx=argument2;
_yy=argument3;
_dir=argument4;
_B=-argument5;



_A=sqrt(sqr(_xx - _x) + sqr(_yy - _y)); //find the distance from the center to the point
if  (_x - _xx) != 0 //decide whether we are looking cos angle 1 or 2.
{
_A*=sign(_x - _xx)
}
if  (_y - _yy) != 0 //decide whether we are looking sin angle 1 or 2.
{
_A*=sign(_y - _yy)
}

_angle = 180 - abs(arcsin((_A*(-sin(degtorad(_dir)))/_B))); //find the missing angle.
_C = (sqr(_A) + sqr(_B)) - (2*_A * _B * cos(degtorad(_angle))) //find the distance squared

return (sqrt(_C)) //return the distance.


  • 0

#9 Binsk

Binsk

    -Insane Kid-

  • GMC Member
  • 3111 posts
  • Version:GM:Studio

Posted 04 April 2013 - 04:06 AM

Aaah, I see. Thank you, all of you. You guys were a great help, cheers! :)


Edited by Scyler_27, 06 April 2013 - 07:36 PM.

  • 0

NEWS FLASH! You can't get to heaven by your works. No chance. Hopeless. Give up now. No amount of works will get you anywhere, in fact all your efforts have been botched years ago because you already missed perfection the first time you lied, stole, or performed any of those other lovelies.

 

That said, do you want to go to heaven? Not the hellish picture given by cartoons. You know, the nightmarish one where you are renting a cloud and harp? I mean, holy crap, where did that come from? I am talking about a bonafide city, people, designed by God no less. Heck, read John's Revelation 21 if you want details. Getting to heaven is extremely simple, actually, the how and why is spelled out in Paul's letter to the Romans, summarized in Romans 10:9,10. God lived a perfect life and died in your place, covering every one of your mistakes and failures past and future. All you have to do is believe that He did what he said He did and accept the gift. Bam, you're saved. Done deal. Complicated, no? Questions? PM me.


#10 A saurus1

A saurus1

    GMC Member

  • Sandbox Moderators
  • 1609 posts
  • Version:GM8.1

Posted 06 April 2013 - 11:08 PM

/*
line_circle_intersection(cx, cy, r, px, py, vx, vy)
Finds the intersection between a line [px,py] + t * [vx,vy], where [vx,vy] is a unit vector, and a circle centered at (sx,sy) with radius r
Returns the distance from the point to the circle and the point of intersection (returnX, returnY)
Script made by TheSnidr
www.thesnidr.com
*/
var a, b, d, dp, xx, yy;
xx = argument0 - argument3
yy = argument1 - argument4
a = argument5
b = argument6
dp = xx * a + yy * b
d = sqr(dp) + sqr(argument2) - sqr(xx) - sqr(yy)
if d < 0 {return 0}
d = sqrt(d)
if dp < d   {dp += d if dp < 0{return 0}}
else        {dp -= d}
returnX = argument3 + a * dp
returnY = argument4 + b * dp
return dp;

Simplified a line_sphere intersection script to 2D. Haven't tested it after the conversion, but I think it should work

 

EDIT:
If you know the point is inside the circle, the script can be simplified to this:

 

/*
line_circle_intersection(cx, cy, r, px, py, vx, vy)
Finds the intersection between a line [px,py] + t * [vx,vy], where [vx,vy] is a unit vector, and a circle centered at (sx,sy) with radius r
Returns the distance from the point to the circle and the point of intersection (returnX, returnY)
Script made by TheSnidr
www.thesnidr.com
*/
var a, b, d, dp, xx, yy;
xx = argument0 - argument3
yy = argument1 - argument4
dp = xx * argument5 + yy * argument6
d = dp + sqrt(sqr(dp) + sqr(argument2) - sqr(xx) - sqr(yy))
returnX = argument3 + argument5 * d
returnY = argument4 + argument6 * d
return d;

I folowed you until you started taking the square root of the dot product of the two vectors. Out of purely mathematical interest, could you explain how this works?


  • 1

#11 TheSnidr

TheSnidr

    Heavy metal viking dentist

  • GMC Elder
  • 3345 posts
  • Version:GM:Studio

Posted 07 April 2013 - 08:22 AM

Made this, it might make it more clear:

utennavng.png


  • 0

#12 A saurus1

A saurus1

    GMC Member

  • Sandbox Moderators
  • 1609 posts
  • Version:GM8.1

Posted 07 April 2013 - 12:03 PM

Ah... I understand.

 

Though I fail to see what (5) did for you. Seems like you used in in the begining of X and then discarded it.


  • 0

#13 TheSnidr

TheSnidr

    Heavy metal viking dentist

  • GMC Elder
  • 3345 posts
  • Version:GM:Studio

Posted 07 April 2013 - 01:22 PM

I don't discard anything, I merely simplify (notice how distance is both outside the sqrt and inside the sqr). (5) is a combination of (1) and (2), and is a great example of how trigonometry and vector math are intertwined!


  • 0