Showing posts with label triangle. Show all posts
Showing posts with label triangle. Show all posts

Saturday, June 3, 2017

Trinagle by side lenghts

Construct a triangle by its side lengths.
first length given will be used as the base.
Usage:


polygon(TrinagleBySides(rands(5, 30, 1)[
  0], rands(5, 30, 1)[0], rands(5,
  30, 1)[0]));

function TrinagleBySides(A, B, C) =
isValidTriangle(A, B, C) ? [
 [0, 0], [A, 0], CCintersection(A, B, C)
  ] : [];

function isValidTriangle(a, b, c) = (
a >  max(b, c)) ?  (a < b + c) : 
(b > max(a,  c)) ? (b < a + c) :
 /*else*/          (c < a +  b);

function CCintersection(A, r0, r1) =
let (p0 = [0, 0], p1 = [A, 0], d = norm(
  p1 - p0), a = (r0 * r0 - r1 * r1 +
  d * d) / (2 * d), h = sqrt(r0 * r0 -
  a * a), p2 = [p0.x + a *
  (p1.x - p0.x) / d, p0.y + a * (p1.y -
    p0.y) / d], p3 = [p2.x + h *
  (p1.y - p0.y) / d, p2.y + h * (p1.x -
    p0.x) / d])
p3;
 


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]);

 
 

Friday, May 12, 2017

Surface normal of a triangle

 
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(len3(v),1e-64);
function len3(v) =len(v)>1?
sqrt(addl([for(i=[0:len(v)-1])pow(v[i], 2)]))
:len(v)==1?v[0]:v; 
function addl(l,c=0)= len(l)-1 >c?l[c]+addl(l,c+1):l[c];

 

Thursday, May 11, 2017

Herons Formula

Herons formula returns the area of an triangle by the length of its sides.

function heron(a,b,c)=
let(s=(a+b+c)/2)
sqrt(abs(s*(s-a)*(s-b)*(s-c)));