Circles of given radius through two points: Difference between revisions

m
lambdatalk minor edit
(adding lambdatalk)
m (lambdatalk minor edit)
Line 1,885:
=={{header|Lambdatalk}}==
<lang scheme>
input: OP1=(x1,y1), OP2=(x2,y2), r
output: OC = OH + HC
where OH = (OP1+OP2)/2
and HC = j*|HC|
where j is the unit vector rotated -90° from P1P2
and |HC| = √(r^2 - (|P1P2|/2)^2) if exists
 
{def circleby2points
{lambda {:x1 :y1 :x2 :y2 :r}
Line 1,892 ⟶ 1,899:
then same points
else {let { {:r :r}
{:pxvx {- :x2 :x1}} {:pyvy {- :y2 :y1}} // v = P1P2
{:hx {/ {+ :x1 :x2} 2}} {:hy {/ {+ :y1 :y2} 2}} } // h = OH
{let { {:r :r} {:pxvx :pxvx} {:pyvy :pyvy} {:hx :hx} {:hy :hy} // closure
{:normd {sqrt {+ {* :px :px} {* :py :py}}} } } // d = |P1P2|
{if {> :normd {* 2 :r}} // d > diam
then no circle, points are too far apart
else {if {= :normd {* 2 :r}} // d = diam
then one circle: opposite ends of diameter with centre (:hx,:hy)
else {let { {:r :r} {:hx :hx} {:hy :hy} // closure
{:jx {- {/ :pyvy :normd}}} {:jy {/ :pxvx :normd}} // j unit -90° to P1P2
{:d {sqrt {- {* :r :r} {/ {* :normd :normd} 4}}}} } // |HC|
two circles: {br}({-+ :hx {* :d :jx}},{+ :hy {* :d :jy}}) // OH + j*|HC|
{br}({+- :hx {* :d :jx}},{- :hy {* :d :jy}}) }}}}}}}}} // OH - j*|HC|
}}}}}}}}}
-> circleby2points
 
{circleby2points -1 0 1 0 0.5}
-> no circle:
points are too far apart
 
{circleby2points -1 0 1 0 1}
-> one circle:
opposite ends of diameter with centre (0,0)
 
{circleby2points -1 0 1 0 {sqrt 2}}
-> two circles:
(0,1.0000000000000002)
(0,-1.0000000000000002)
 
rosetta's task:
 
{circleby2points 0.1234 0.9876 0.8765 0.2345 2.0}