Jump to content

Circles of given radius through two points: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 20:
;Ref:
* [http://mathforum.org/library/drmath/view/53027.html Finding the Center of a Circle from 2 Points and Radius] from Math forum @ Drexel
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>CircleCenter(x1, y1, x2, y2, r){
d := sqrt((x2-x1)**2 + (y2-y1)**2)
x3 := (x1+x2)/2 , y3 := (y1+y2)/2
cx1 := x3 + sqrt(r**2-(d/2)**2)*(y1-y2)/d , cy1:= y3 + sqrt(r**2-(d/2)**2)*(x2-x1)/d
cx2 := x3 - sqrt(r**2-(d/2)**2)*(y1-y2)/d , cy2:= y3 - sqrt(r**2-(d/2)**2)*(x2-x1)/d
if (d = 0)
return "No circles can be drawn, points are identical"
if (d = r*2)
return "points are opposite ends of a diameter center = " cx1 "," cy1
if (d = r*2)
return "points are too far"
if (r <= 0)
return "radius is not valid"
if !(cx1 && cy1 && cx2 && cy2)
return "no solution"
return cx1 "," cy1 " & " cx2 "," cy2
}</lang>
Examples:<lang AutoHotkey>data =
(
0.1234 0.9876 0.8765 0.2345 2.0
0.0000 2.0000 0.0000 0.0000 1.0
0.1234 0.9876 0.1234 0.9876 2.0
0.1234 0.9876 0.8765 0.2345 0.5
0.1234 0.9876 0.1234 0.9876 0.0
)
 
loop, parse, data, `n
{
obj := StrSplit(A_LoopField, " ")
MsgBox, CircleCenter(obj[1], obj[2], obj[3], obj[4], obj[5])
}</lang>
Outputs:<pre>0.1234 0.9876 0.8765 0.2345 2.0 > 1.863112,1.974212 & -0.863212,-0.752112
0.0000 2.0000 0.0000 0.0000 1.0 > points are opposite ends of a diameter center = 0.000000,1.000000
0.1234 0.9876 0.1234 0.9876 2.0 > No circles can be drawn, points are identical
0.1234 0.9876 0.8765 0.2345 0.5 > no solution
0.1234 0.9876 0.1234 0.9876 0.0 > No circles can be drawn, points are identical
</pre>
 
=={{header|C}}==
299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.