package require PWI_Glyph 2.17.3 # Code Starts here #===================================== # set iDebug 0 set creator [pw::Application begin Create] # # Coords of 3 pts # if { [catch {set pt1 [pw::Display selectPoint -description \ "Select first point." ]}]} { puts "Script aborted." exit } if { [catch {set pt2 [pw::Display selectPoint -description \ "Select second point." ]}]} { puts "Script aborted." exit } if { [catch {set pt3 [pw::Display selectPoint -description \ "Select third point." ]}]} { puts "Script aborted." exit } set x1 [ lindex $pt1 0] set y1 [ lindex $pt1 1] set z1 [ lindex $pt1 2] if { $iDebug == 1 } { puts [ format "Pt1 %f, %f, %f" $x1 $y1 $z1] } set x2 [ lindex $pt2 0] set y2 [ lindex $pt2 1] set z2 [ lindex $pt2 2] if { $iDebug == 1 } { puts [ format "Pt2 %f, %f, %f" $x2 $y2 $z2] } set x3 [ lindex $pt3 0] set y3 [ lindex $pt3 1] set z3 [ lindex $pt3 2] if { $iDebug == 1 } { puts [ format "Pt3 %f, %f, %f" $x3 $y3 $z3] } # create points at the 3 input points # set p1 [pw::Point create] # $p1 setPoint $pt1 # set p2 [pw::Point create] # $p2 setPoint $pt2 # set p3 [pw::Point create] # $p3 setPoint $pt3 # calculate stuff # vector 1 set vectorAB_i [ expr { $x2 - $x1 } ] set vectorAB_j [ expr { $y2 - $y1 } ] set vectorAB_k [ expr { $z2 - $z1 } ] set vectorAB_length [ expr { $vectorAB_i*$vectorAB_i + \ $vectorAB_j*$vectorAB_j + \ $vectorAB_k*$vectorAB_k } ] set vectorAB_length [ expr { sqrt($vectorAB_length) } ] if { $iDebug == 1 } { puts [ format "Vector AB: i %f j %f k %f, length %f" $vectorAB_i $vectorAB_j $vectorAB_k $vectorAB_length] } # vector 2 set vectorBC_i [ expr { $x3 - $x2 } ] set vectorBC_j [ expr { $y3 - $y2 } ] set vectorBC_k [ expr { $z3 - $z2 } ] set vectorBC_length [ expr { $vectorBC_i*$vectorBC_i + \ $vectorBC_j*$vectorBC_j + \ $vectorBC_k*$vectorBC_k } ] set vectorBC_length [ expr { sqrt($vectorBC_length) } ] if { $iDebug == 1 } { puts [ format "Vector BC: i %f j %f k %f, length %f" $vectorBC_i $vectorBC_j $vectorBC_k $vectorBC_length] } # vector 3 set vectorCA_i [ expr { $x1 - $x3 } ] set vectorCA_j [ expr { $y1 - $y3 } ] set vectorCA_k [ expr { $z1 - $z3 } ] set vectorCA_length [ expr { $vectorCA_i*$vectorCA_i + \ $vectorCA_j*$vectorCA_j + \ $vectorCA_k*$vectorCA_k } ] set vectorCA_length [ expr { sqrt($vectorCA_length) } ] if { $iDebug == 1 } { puts [ format "Vector CA: i %f j %f k %f, length %f" $vectorCA_i $vectorCA_j $vectorCA_k $vectorCA_length] } # sort the lengths in increasing order set sidelengths [ list $vectorAB_length $vectorBC_length $vectorCA_length ] set sidelengths [ lsort -real $sidelengths ] # check if collinear set l1 [ lindex $sidelengths 0 ] set l2 [ lindex $sidelengths 1 ] set sum1 [ expr { $l1 + $l2 } ] set l3 [ lindex $sidelengths 2 ] if { $iDebug == 1 } { puts [ format " Sorted lengths: %f, %f, %f" $l1 $l2 $l3 ] } if { $l3 >= $sum1 } { puts "Points are collinear" exit -1 } # calculate center of circle set l_a $vectorBC_length set l_b $vectorCA_length set l_c $vectorAB_length set l_s [ expr { $l_a + $l_b + $l_c } ] set l_s [ expr { $l_s/2.0 } ] set radius [ expr { $l_a*$l_b*$l_c } ] set radius [ expr { $radius/4.0 } ] set denom [ expr { $l_s*($l_s - $l_a)*($l_s - $l_b)*($l_s - $l_c) } ] set radius [ expr { $radius/sqrt($denom) } ] if { $iDebug == 1 } { puts [ format "s: %f" $l_s] puts [ format "Radius: %s" $radius ] } set b1 [ expr { $l_a*$l_a*($l_b*$l_b + $l_c*$l_c - $l_a*$l_a) } ] set b2 [ expr { $l_b*$l_b*($l_a*$l_a + $l_c*$l_c - $l_b*$l_b) } ] set b3 [ expr { $l_c*$l_c*($l_b*$l_b + $l_a*$l_a - $l_c*$l_c) } ] set sum_b [ expr { $b1 + $b2 + $b3 } ] set Px [ expr { $b1*$x1 + $b2*$x2 + $b3*$x3 } ] set Py [ expr { $b1*$y1 + $b2*$y2 + $b3*$y3 } ] set Pz [ expr { $b1*$z1 + $b2*$z2 + $b3*$z3 } ] set Px [ expr { $Px / $sum_b } ] set Py [ expr { $Py / $sum_b } ] set Pz [ expr { $Pz / $sum_b } ] if { $iDebug == 0 } { puts [ format "Px, Py, Pz: %f, %f, %f" $Px $Py $Pz ] } # create the point at the center set pCenter [pw::Point create] set pData [ list $Px $Py $Pz ] $pCenter setPoint $pData $creator end