Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Nearest Point on a Circle
- scratchcraft789
-
Scratcher
24 posts
Nearest Point on a Circle
I have a point on a 2D coordinate plane. There is an imaginary circle around some other origin point, and I need two expressions (one for each axis) to find the nearest point from my first coordinates that is on that circle's perimeter. Like this:
when green flag clicked
set [radius v] to (100)
set [origin x v] to [0]
set [origin y v] to [0]
set [x v] to [100]
set [y v] to [100]
go to x: [expression 1] y: [expression 2]
set [x v] to (x position)
set [y v] to (x position)
Last edited by scratchcraft789 (Sept. 26, 2026 13:42:37)
- deck26
-
Scratcher
1000+ posts
Nearest Point on a Circle
The nearest point will always be on a line from the current position to the centre of the circle which is presumably known. (Extend the line if within the circle.)
You can just point towards the centre, go to that point, turn 180 degrees and move R steps where R is the radius. You may want to save the direction the sprite was pointing before doing all this so you can then point it in that direction again.
You can just point towards the centre, go to that point, turn 180 degrees and move R steps where R is the radius. You may want to save the direction the sprite was pointing before doing all this so you can then point it in that direction again.
- scratchcraft789
-
Scratcher
24 posts
Nearest Point on a Circle
The nearest point will always be on a line from the current position to the centre of the circle which is presumably known. (Extend the line if within the circle.)It's a good idea, but I would have to make a point towards 0, 0 expression and use multiple blocks, and I'm trying to make everything fit in one expression.
You can just point towards the centre, go to that point, turn 180 degrees and move R steps where R is the radius. You may want to save the direction the sprite was pointing before doing all this so you can then point it in that direction again.
- deck26
-
Scratcher
1000+ posts
Nearest Point on a Circle
Place a hidden sprite at the centre of the circle and you can easily just point to that/
- 10goto10
-
Scratcher
1000+ posts
Nearest Point on a Circle
So, you're just wanting to find a third point (?).
Sorry for changing the variable names but this should do it:
as a single expression
Sorry for changing the variable names but this should do it:
when @greenFlag clicked
set [R v] to [100]
set [Cx v] to [20]
set [Cy v] to [-20]
set [Px v] to [200]
set [Py v] to [-150]
set [m v] to ([sqrt v] of ((((Px) - (Cx)) * ((Px) - (Cx))) + (((Py) - (Cy)) * ((Py) - (Cy))))::operators)
set [Ix v] to ((Cx) + ((R) * (((Px) - (Cx)) / (m))))
set [Iy v] to ((Cy) + ((R) * (((Py) - (Cy)) / (m))))
go to x\: (Ix) y\: (Iy)
as a single expression
go to x\: ((Cx) + ((R) * (((Px) - (Cx)) / ([sqrt v] of ((((Px) - (Cx)) * ((Px) - (Cx))) + (((Py) - (Cy)) * ((Py) - (Cy))))::operators)))) y\: ((Cy) + ((R) * (((Py) - (Cy)) / ([sqrt v] of ((((Px) - (Cx)) * ((Px) - (Cx))) + (((Py) - (Cy)) * ((Py) - (Cy))))::operators))))
- TheWhiteShaddow
-
Scratcher
62 posts
Nearest Point on a Circle
I don't know if any of these already work for you, but if not, here's how I would do it: (assuming my mental algebra is up to code)
Assuming that
This is based on some math I learned while trying to make a soft-body physics engine, and some notes from my college algebra class.
Assuming that
(r)is your circle's radius,
(cX)
(cY)are its center coordinates,
(pX)
(pY)are your other point's coordinates, then
set [closestPointX v] to ((cX) + ((r) * ([cos v] of ([atan v] of ([abs v] of (((pX) - (cX)) / ((pY) - (cY)))::operators)::operators)::operators)))should work. (I think) This is the most compact way I could come up with to write it.
set [closestPointY v] to ((cY) + ((r) * ([sin v] of ([atan v] of ([abs v] of (((pX) - (cX)) / ((pY) - (cY)))::operators)::operators)::operators)))
This is based on some math I learned while trying to make a soft-body physics engine, and some notes from my college algebra class.
- Discussion Forums
- » Help with Scripts
-
» Nearest Point on a Circle