I'm trying to write a program on CNC. Basically I have circular arc starting x, y , radius and finishing x, y also I know the direction of the arc clockwise or cc. So I need to find out the value of y on the arc at the specific x position. What is the best way to do that? I found similar problem on this website here. But i not sure how to get angle a.
-
Do you just want y_random or do you want the angle as well? – francium Mar 24 '16 at 21:27
-
x=R cos(theta), so theta = arccos(x/R), y=R sin(theta). You may need to may need to make sure you are in the correct quadrant. – Obromios Mar 24 '16 at 21:28
-
sorry, i haven't mention this but its circular arc. – aidas Mar 24 '16 at 23:31
-
francium i just want to get y_random. – aidas Mar 25 '16 at 00:55
2 Answers
equation of a circle is x^2 + y^2 = r^2
in your case, we know x_random
and R
substituting in knows we get,
x_random ^ 2 + y_random ^ 2 = R ^ 2
and solving for y_random
get get
y_random = sqrt( R ^ 2 - x_random ^ 2 )
Now we have y_random
Edit: this will only work if your arc is a circular arc and not an elliptical arc
to adapt this answer to an ellipse, you'll need to use this equation, instead of the equation of a circle
( x ^ 2 / a ^ 2 ) + ( y ^ 2 / b ^ 2 ) = 1
, where a
is the radius along the x axis
and b
is the radius along y axis
Simple script to read data from a file called data.txt
and compute a series of y_random
values and write them to a file called out.txt
import math
def fromFile():
fileIn = open('data.txt', 'r')
output = ''
for line in fileIn:
data = line.split()
# line of data should be in the following format
# x h k r
x = float(data[0])
h = float(data[1])
k = float(data[2])
r = float(data[3])
y = math.sqrt(r**2 - (x-h)**2)+k
if ('\n' in line):
output += line[:-1] + ' | y = ' + str(y) + '\n'
else:
output += line + ' | y = ' + str(y)
print(output)
fileOut = open('out.txt', 'w')
fileOut.write(output)
fileIn.close()
fileOut.close()
if __name__ == '__main__':
fromFile()
data.txt
should be formatted as such
x0 h0 k0 r0
x1 h1 k1 r1
x2 h2 k2 r2
... for as many lines as required

- 2,384
- 3
- 20
- 29
-
i thought equation for circle is (x – h)^2 + (y – k)^2 = r^2. Am i wrong? h, k centre of a circle. – aidas Mar 24 '16 at 23:33
-
@aidas h and k are used to translate the circle along the x and y axis. If you don't translate the circle, h=0, k=0, then you get x^2 + y^2 = r^2 – francium Mar 25 '16 at 00:26
-
but in my case h and k isn't 0. i could offset origin to zero with every arc in the program i'm making. but i'm hoping that there's cleaner method. – aidas Mar 25 '16 at 00:47
-
@aidas well for every arc the calculation is still the same, only difference is the numbers you plug in. You could easily write a simple program (python for example) that will accept your numbers and quickly compute the result. – francium Mar 25 '16 at 00:56
-
@fracium could you show me how its done? python is fine. just centre of circle is unknown. i found equation for it [here](http://math.stackexchange.com/questions/27535/how-to-find-center-of-an-arc-given-start-point-end-point-radius-and-arc-direc). its just my math skills to solve this one isn't good enough. – aidas Mar 25 '16 at 07:17
-
At first you have to find circle equation. Let's start point Pst = (xs,ys)
, end point Pend = (xend,yend)
For simplicity shift all coordinates by (-xs, -ys)
, so start point becomes coordinate origin.
New Pend' = (xend-xs,yend-ys) = (xe, ye)
, new 'random point' coordinate is xr' = xrandom - xs
, unknown circle center is (xc, yc)
xc^2 + yc^2 = R^2 {1}
(xc - xe)^2 + (yc-ye)^2 = R^2 {2} //open the brackets
xc^2 - 2*xc*xe + xe^2 + yc^2 - 2*yc*ye + ye^2 = R^2 {2'}
subtract {2'} from {1}
2*xc*xe - xe^2 + 2*yc*ye - ye^2 = 0 {3}
yc = (xe^2 + ye^2 - 2*xc*xe) / (2*ye) {4}
substitute {4} in {1}
xc^2 + (xe^2 + ye^2 - 2*xc*xe)^2 / (4*ye^2) = R^2 {5}
solve quadratic equation {5} for xc, choose right root (corresponding to arc direction), find yc
having center coordinates (xc, yc), write
yr' = yc +- Sqrt(R^2 -(xc-xr')^2) //choose right sign if root exists
and finally exclude coordinate shift
yrandom = yr' + ys

- 77,366
- 5
- 53
- 86