Showing posts with label circle. Show all posts
Showing posts with label circle. Show all posts

Wednesday, June 21, 2017

Great Circle / Concentric Small Circle By Two Lat Long Points

A great circle is any circle drawn on a  sphere with a center at the center of the sphere and thus the same radius.

function face_normal(point_a,point_b,point_c)=
let(u=point_a-point_b,v=point_a-point_c)
un([u[1]*v[2]-u[2]*v[1],u[2]*v[0]-
u[0]*v[2],u[0]*v[1]-u[1]*v[0]]);

function un(v)=v/max(norm(v),1e-64);

function look_at(p,o=[0,0,0],up=[0,0,1])=
let(
a=up,
b=p-o,
c=cross(a,b) ,
d=angle(a,b))
[d,c];

function angle (a,b)=
atan2(
sqrt((cross(a, b)*cross(a, b))), 
(a* b)  );

function rnd(a = 1, b = 0, s = []) =
s == [] ?
(rands(min(a, b), max(
a, b), 1)[0]) :
(rands(min(a, b), max(a, b), 1, s)[0]);

function Great_circle_axis(p1,p2,r)=
let(v1=face_normal([0,0,0],p1,p2))
v1;

module Great_circle(p1,p2,r){
v1=face_normal([0,0,0],p1,p2);
look_at(v1)torus(r,0.05);
}
module Concentric_small_circle(p1,p2,r,r2){
v1=face_normal([0,0,0],p1,p2);
look_at(v1)translate([0,0,sign(r2)*r*cos(90*(r2/r))])
torus(r*sin(90*(r2/r)),0.05);
}

module torus(r1,r2)
{rotate_extrude($fn=40)translate([r1,0,0])circle(r2,$fn=12);
}
module look_at(lookpoint,origin=[0,0,0],rotatefrom=[0,0,1]){
rotations=look_at(lookpoint,origin ,rotatefrom);
rotate(rotations[0],rotations[1]) children();
}
radius=5;
small_radius=radius*rnd();
#sphere(5,$fn=40);
lat1=rnd(-90,90);
long1=rnd(0,360);
lat2=rnd(-90,90);
long2=rnd(0,360);

p1= ([sin(long1),cos(long1)*sin(lat1),cos(lat1)])*radius;
p2= ([sin(long2),cos(long2)*sin(lat2),cos(lat2)])*radius;
echo(p1,p2);
translate(p1) sphere(0.3,$fn=40);
translate(p2) sphere(0.3,$fn=40);

Great_circle(p1,p2,radius);
Concentric_small_circle(p1,p2,radius,-small_radius);
Concentric_small_circle(p1,p2,radius,small_radius);


 

Tuesday, June 20, 2017

Great-Circle Distance between two lat long positions

This formula doesn't count the flattening of the earth at the poles. 
The earth has been approximated to a sphere with a radius of 6378 km.
 


function Great_circle_distance(latA,longA,latB,longB) =   
acos ( 
sin (latA) * sin (latB) +
cos (latA) * cos (latB) * cos (longB - longA) 
)*111.195  ;//(6371/57.295779513)

echo (Great_circle_distance(48.8566, -2.3522,52.5200 , -13.4050) );
//INFERNAL ;)
echo (Great_circle_distance(0, 90, 0, 0) );
 
 

Area Of A Circle By Radius


function Area_Of_A_Circle_By_Radius(r)=r*r*PI;

Saturday, June 10, 2017

Thursday, June 8, 2017

Circle Path

Generates a path in the shape of a circle

 
function circle(r=100,steps=100)=
[ for(t=[0:1/steps:1])
let(  x =r* sin(t*360),   y =r*cos(t*360)  )
[x,y] ];
// function close adds a last point equal yo the first
function close(p)= concat(p,[p[0]]);

module polyline(p) {for(i=[0:max(0,len(p)-2)])line(p[i],p[i+1]);} module line(p1, p2 ,width=0.5) { hull() { translate(p1) sphere(width); translate(p2) sphere(width); } } polyline(close(circle()));

Tuesday, June 6, 2017

Recursive Tangential Circle Approximation To Any Mix Of 3 Points, Lines, Circles Or Rectangles

Recursively try to find circles that touch outside three random features
of either point, line, circle or rectangle. 




Approximation start at a seed point and steps towards the smaller error 
until the summed error is smaller than a threshold.
First attempt starts at features common average  midpoint. 

If approximation error is smaller than threshold  the search is halted. 

Next starting points from around perimeter are tried and then a bunch of 
random starting  points.

Solution inside circles and squares hare not handled.
Results are not scientific exact, enough for construction of drawing.

Some unsolved edge cases fail, reducing overall usefulness.


Demo choose random features
Usage:



threshold = 1e-5;
workarea = 100;


function  AcmeSolver(f0, f1, f2, ifind = undef, maxi = 6000) =
let (
find = ifind == undef ? 
    (featurecenter(f0)   + featurecenter(f1)  
    + featurecenter(f2)  ) /3: ifind, 
d0 =distancetofeature(f0, find), 
nf0 = normaltofeature(f0, find),
d1 = distancetofeature(f1, find), 
nf1 = normaltofeature(f1, find),
d2 = distancetofeature(f2, find), 
nf2 = normaltofeature(f2, find),
sum = abs(d0 - d1) + abs(d1 - d2) + abs(d2 - d0), 
avrg = (d0 + d2 +    d1) / 3,  
cf0 = (find + nf0 * (max(0, d0) - avrg) * 0.999),  
cf1 = (    find + nf1 * (max(0, d1) - avrg) * 0.999),  
cf2 = (find + nf2 * (    max(0, d2) - avrg) * 0.999),  
newfind = (cf0 + cf1 + cf2) / 3 )
sum < threshold || maxi < 0 ? 
[find, avrg,   sum]
:  AcmeSolver(f0,
f1, f2, newfind, maxi - 1);

function featurecenter(feat) = 
len(feat) == 2 && len(feat[0]) ==  undef ? feat :
len(feat) == 3 && len(feat[0]) == undef ? [feat.x,feat.y] : 
len(feat) == 2 && len(feat[0]) == 2 ? (feat[0] + feat[1]) /2 :
len(feat) == 2 && len(feat[0]) == 2 ? 
(feat[0] + feat[1]+feat[2] + feat[3]) /  4 : [0, 0];

function distancetofeature(feat, find) = 
len(feat) == 2 && len(feat[0]) ==  undef ? norm(feat - find) : 
len(feat) == 3 && len(feat[0]) == undef ?  
distancetocircle(feat, find) : 
len(feat) == 2 && len(feat[0]) == 2 ?  distancetoline(feat, find) : 
len(feat) == 4 && len(feat[0]) == 2 ?  distancetosquare(feat, find) :
0;

function normaltofeature(feat, find) = 
len(feat) == 2 && len(feat[0]) ==  undef ? normaltopoint(feat, find) :
 len(feat) == 3 && len(feat[0]) ==  undef ? 
normaltocircle(feat, find) : 
len(feat) == 2 && len(feat[0]) ==  2 ? normaltoline(feat, find) :
len(feat) == 4 && len(feat[0]) ==  2 ? normaltosquare(feat, find) :
 0;

function distancetopoint(point, find) = norm(point - find);

function distancetocircle(circle, find) = 
norm([circle.x, circle.y] -  find) - circle[2];

function distancetoline(line, p) =
let (a = line[0], b = line[1], 
pa = p - a, ba = b - a, h =  ((pa * ba) /
  (ba * ba)))norm(pa - ba * h);

function distancetosquare(square, p) =min (
distancetosegment([square[0],square[1]],p),
distancetosegment([square[1],square[2]],p),
distancetosegment([square[2],square[3]],p),
distancetosegment([square[3],square[0]],p)

);

function distancetosegment(line, p) =
let (a = line[0], b = line[1], pa = p - a, ba = b - a,
 h =  clamp((pa * ba) /  (ba * ba)))
norm(pa - ba * h);
function clamp(a, b = 0, c = 1) = min(max(a, b), c); 

function normaltopoint(point, find) = 
(point - find) / norm(point -  find);

function normaltocircle(circle, find) = 
([circle.x, circle.y] - find) /norm([circle.x, circle.y] - find);

function normaltoline(line, find) =
let (a = line[0], b = line[1], dtl2 = distancetoline(line, find))
un([distancetoline(line, find + [-1, 0]) -
 dtl2, distancetoline(line,  find + [0, -1]) - dtl2]);

function normaltosegment(line, find) =
let (a = line[0], b = line[1],c=un(a-b))[-c.y,c.x];
 

function normaltosquare(square, find) =
let (  base = distancetosquare(square, find))
un([distancetosquare(square, find + [-1, 0]) - 
base, distancetosquare(square, find+ [0, -1]) - base]);

function un(v) = v / max(1e-15, norm(v));

// demo code

function allines(f0,f1,f2)=isline(feature0)
&&isline(feature1)&&isline(feature2);
function isline(feat)=len(feat) == 2 
&& len(feat[0]) == 2 ?true:false;
function randomfeature() =
let (s0 = round( rnd(3)
))
(s0 == 0) ? [rnd(workarea), rnd(workarea), rnd(1, workarea * 0.5)] :
(s0 == 1) ? [rnd(workarea), rnd(workarea)] : 
(s0 == 2) ? [[rnd(  workarea), rnd(workarea)],
 [rnd(workarea), rnd(workarea)]] :
(s0 == 3) ? let(
p1=[rnd(workarea), rnd(workarea)],
p2=p1+[rnd(workarea)/2, rnd(workarea)/2],
r=rnd(360),p3=(p1+p2)/2)
[rotp(r,p1,p3),rotp(r,[p2.x,p1.y],p3),
rotp(r,p2,p3),rotp(r,[p1.x,p2.y],p3) ] :
 ["err"];

function rotp(r,ip,p3)=
let( p=ip-p3, s = sin(r),
  c = cos(r),
    xnew = p.x * c - p.y * s,
    ynew = p.x * s + p.y * c)
[xnew,ynew]+p3
;
module showfeature(feat) {
  color("black") {
    if (len(feat) == 2 && len(feat[0]) == undef) {
      echo(" point ", feat);
      translate(feat) sphere(2);
    }
    if (len(feat) == 3 && len(feat[0]) == undef) {
      echo(" circle ", feat);
      linear_extrude(1)translate([feat.x, feat.y]) difference() {
        circle(feat[2]);
        circle(feat[2] - 1);
      }
    }
    if (len(feat) == 2 && len(feat[0]) == 2) {
      echo(" line ", feat);
      linear_extrude(1)hull() {
        translate(feat[0]) circle(1);
        translate(feat[1]) circle(1);
      }
      n1 = un(feat[0] - feat[1]);
      linear_extrude(0.2)hull() {
        translate(feat[0] + n1 * workarea) circle(0.2);
        translate(feat[1] - n1 * workarea) circle(0.2);
      }
    }
if (len(feat) == 4 && len(feat[0]) == 2) {
      echo(" square ", feat);
      linear_extrude(1)hull() {
        translate(feat[0]) circle(0.5);
        translate(feat[1]) circle(0.5);
      }
 linear_extrude(1)hull() {
        translate(feat[1]) circle(0.5);
        translate(feat[2]) circle(0.5);
      }
      linear_extrude(1)hull() {
        translate(feat[2]) circle(0.5);
        translate(feat[3]) circle(0.5);
      }
      linear_extrude(1)hull() {
        translate(feat[3]) circle(0.5);
        translate(feat[0]) circle(0.5);
      }
     
    }
  }
}
module ring(d, i = 1) {
  linear_extrude(i)
  {
    translate(d[0]) difference() {
      circle(d[1], $fn = 60);
      circle(max(0,d[1] - 1), $fn = 60);
    }
  }
}

function rnd(a = 1, b = 0, s = []) = s == [] ? 
(rands(min(a, b), max(
  a, b), 1)[0]) : (rands(min(a, b), max(a, b), 1, s)[0]);



feature0 = randomfeature();
feature1 = randomfeature();
feature2 = randomfeature();
showfeature(feature0);
showfeature(feature1);
showfeature(feature2);
// try fo fit from midpoint
d =  AcmeSolver(feature0, feature1, feature2);
//[center,radius, fit]

if (d[2] < threshold &&!allines(feature0, feature1, feature2)){
echo(" Found Internal ", d);
  color("red") ring(d, 1.5);
}
else {
  color("yellow") ring(d);
  if (d[2] < workarea*0.1) {
    echo(" Found no internal solution in time. Best fit : ", d[2]);
    echo(d);    
  }
  else {
    echo(" Found no internal solution in time. Best approximation : ");
    echo(d);   
  }

// try to fit from ouside
for (i = [45: 90: 360]) {
  cme = [sin(i) * workarea * 10, cos(i) * workarea * 10];
  de =  AcmeSolver(feature0, feature1, feature2, cme,1500);
  if (norm(d[0] - de[0]) > threshold) {
    if (de[2] < threshold) {
      color([1, rnd(), rnd()]) ring(de,0.9);
      echo(" Found External ",de);
    }
    else {
      echo(" Found no external solution in time ");
    }
  }
}
// try to fit from Random State
for (i = [0:  10]) {
  cme = [rnd(-workarea,workarea*2),rnd(-workarea,workarea*2)];
  de =  AcmeSolver(feature0, feature1, feature2, cme,1500);
  if (norm(d[0] - de[0]) > threshold) {
    if (de[2] < threshold) {
      color([1, rnd(), rnd()]) ring(de);
      echo(" Found Random State solution ",de);
    }
    else {
      echo(" Found no Random State solution in time ");
    }
  }
}

}




 

Monday, May 29, 2017

Incircle of a triangle in 3d


Incircle / inscribed circle of a triangle is the largest circle that fits inside a triangle and touches all three sides. 
Usage:


$fn = 50;
point_C = [rnd(-20, 20), rnd(-20, 20), rnd(-20, 20)];
point_B = [rnd(-20, 20), rnd(-20, 20), rnd(-20, 20)];
point_A = [rnd(-20, 20), rnd(-20, 20), rnd(-20, 20)];
center = Incenter(point_A, point_B, point_C);
inradius= point_to_line(center,point_A, point_B);
hull(){translate(point_A) sphere(0.1);
translate(point_B) sphere(0.1);
translate(point_C) sphere(0.1);}
color("red") translate(center) sphere(inradius);
echo(center, inradius);

function Incenter(A, B, C) =
let (
a=norm(B-C),
b=norm(A-C),
c=norm(A-B),
  p = a + b + c, 
  Ox = (a * A.x + b * B.x + c * C.x) / p, 
  Oy = (a * A.y + b * B.y + c * C.y) / p,
  Oz = (a * A.z + b * B.z + c * C.z) / p
    )
[Ox, Oy, Oz];

function point_to_line(p,a,b)=
let(
pa = p - a, 
ba = b - a,
h = clamp( (pa*ba)/ (ba*ba)))
norm( pa - ba*h ) ;
function clamp(a, b = 0, c = 1) = min(max(a, b), c); 

 
function rnd(a = 1, b = 0, s = []) =
s == [] ?
  (rands(min(a, b), max(
    a, b), 1)[0]) :
  (rands(min(a, b), max(a, b), 1, s)[0]);

 
 

Wednesday, May 17, 2017

Circle defined by three points (2d)

Finding the Center of a Circle from Three Points.
Points must not be equal or collinear, - lie on a single straight line 

Usage:


$fn = 20;
point_C = [rnd(-20, -5), rnd(5, 20)];
point_B = [rnd(5, 20), rnd(5, 20)];
point_A = [rnd(5, 20), rnd(-20, -5)];
center = circle_by_three_points(point_A, point_B, point_C);
radius = len3(point_C - center);
translate(point_A) circle(2);
translate(point_B) circle(2);
translate(point_C) circle(2);
color("red") translate(center) circle(radius);
echo(center, radius);


function circle_by_three_points(A, B, C) =
let (
  yD_b = C.y - B.y,  xD_b = C.x - B.x,  yD_a = B.y - A.y,
  xD_a = B.x - A.x,  aS = yD_a / xD_a,  bS = yD_b / xD_b,
  cex = (aS * bS * (A.y - C.y) + 
  bS * (A.x + B.x) -    aS * (B.x + C.x)) / (2 * (bS - aS)),
  cey = -1 * (cex - (A.x + B.x) / 2) / aS + (A.y + B.y) / 2
)
[cex, cey];

// non dependencies helper function for demo

function rnd(a = 1, b = 0, s = []) =s == [] ?
  (rands(min(a, b), max(a, b), 1)[0]) :
  (rands(min(a, b), max(a, b), 1, s)[0]);

function len3(v) = sqrt(pow(v.x, 2) + pow(v.y, 2));