POV-Ray : Newsgroups : povray.advanced-users : shortcut for inner circle : Re: shortcut for inner circle Server Time
29 Jul 2024 14:23:48 EDT (-0400)
  Re: shortcut for inner circle  
From: Josh English
Date: 10 Dec 2001 13:11:59
Message: <3C14FAEC.F66CEECB@spiritone.com>
This code seems to work with all my tests. It uses the law of sines.

#macro Project(a,b)
  #local tmp = (vdot(a,b)/pow(vlength(a),2))*a;
  tmp
#end
#macro Angle(v1,v2)
  degrees(acos(vdot(v1,v2)/(vlength(v1)*vlength(v2))))
#end

#declare p0 = <-1,0,0>;
#declare p1 = <1,0,0>;
#declare p2 = <-1,-1,0>;

#declare p3 = (p0+vnormalize(p1-p0)+p0+vnormalize(p2-p0))/2;
#declare a1 = Angle(p1-p0,p3-p0);
#declare a2 = Angle(p2-p1,p0-p1)/2;
#declare a3 = 180-a1-a2;

#declare lc = sin(radians(a2))*vlength(p1-p0)/sin(radians(a3));
#declare C = p0 + vnormalize(p3-p0)*lc;

sphere { C 0.06 pigment { rgb <1,1,0> } }
#declare D = p0 +Project(p2-p0,C-p0)
sphere { D 0.06 pigment { rgb <1,1,0> } }
disc{ C, vcross(p1-p0,p2-p0), vlength(D-C) pigment { rgb <1,1,0> } }

Josh English
eng### [at] spiritonecom

"W3odzimierz ABX Skiba" wrote:
> 
> I found macro to calculate radius and center of inner circle of 2d triangle. Any
> idea for shorter version of it ? This works fine but I'm asking just for my
> knowledge extending :-) Some vector optimizations ?
> 
> #local f_det=function(a11,a12,a21,a22){(a11*a22)-(a12*a21)};
> 
> // take two lines as pairs of points
> // and calculate intersection
> #macro Cross(x1,y1,x2,y2,x3,y3,x4,y4)
>   #local A1=y2-y1;
>   #local B1=x1-x2;
>   #local C1=f_det(x2,y2,x1,y1);
>   #local A2=y4-y3;
>   #local B2=x3-x4;
>   #local C2=f_det(x4,y4,x3,y3);
>   #local D=f_det(A1,A2,B1,B2);
>   (<f_det(B1,B2,C1,C2),f_det(C1,C2,A1,A2)>/D)
> #end
> 
> // input A,B,C - vertices of 2d triangle
> // output <center.x,center.y,radius>
> #macro Inner_Circle(A,B,C)
>   #local a=vlength(B-A);
>   #local b=vlength(C-B);
>   #local c=vlength(A-C);
>   #local p=(a+b+c)/2;
>   #local R=sqrt((p-a)*(p-b)*(p-c)/p);
>   #local P0=R*vnormalize(vcross(vcross(B-A,C-A),C-B));
>   #local P1=B+P0;
>   #local P2=C+P0;
>   #local R0=R*vnormalize(vcross(vcross(C-B,A-B),A-C));
>   #local R1=C+R0;
>   #local R2=A+R0;
>   #local P=Cross(P1.x,P1.y,P2.x,P2.y,R1.x,R1.y,R2.x,R2.y);
>   <P.x,P.y,R>
> #end
> 
> ABX
> --
> #declare _=function(a,b,x){((a^2)+(b^2))^.5-x}#default {pigment{color rgb 1}}
> union{plane{y,-3}plane{-x,-3}finish{reflection 1 ambient 0}}isosurface{ //ABX
> function{_(x-2,y,1)|_((x+y)*.7,z,.1)|_((x+y+2)*.7,z,.1)|_(x/2+y*.8+1.5,z,.1)}
> contained_by{box{<0,-3,-.1>,<3,0,.1>}}translate z*15finish{ambient 1}}//POV35


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.