# PERL # #### #### All Code by Steve Michel #### # THIS ITERATION TO BEGIN SHADING CALCULATIONS # # Ask user number of objects # # for each object determine # number of polys (tri) per object # # for each poly determine co-ordinates of each point (3 points pre poly) # # # # # # VAriable designations # # $nj = number of objects # $tr = number of triangles per object # # $x = x absolute co-ordinate of given point in 3d space # $y = y absolute co-ordinate of given point in 3d space # $z = z absolute co-ordinate of given point in 3d space # # $u = x co-ordinate of given point on viewing plane # $v = y co-ordinate of given point on viewing plane # # # 3d world is xyz coordinates # 2d viewing plane is uv coordinates # # currently direct from 3d to 2d # # $xc = calculated co-ordinate of given point in 3d space # $yc = calculated co-ordinate of given point in 3d space # $zc = calculated co-ordinate of given point in 3d space # # # # # # # use Tk; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; # print "\n\n"; print "\n\n"; print "\n\n"; #specify 3d coordinates $x[1] = 250-375; $x[2] = 500-375; $x[3] = 250-375; $x[4] = 500-375; $x[5] = 250-375; $x[6] = 500-375; $x[7] = 250-375; $x[8] = 500-375; $x[9] = 500-500; # X axis translation $tx=0; for ($xt=1;$xt<=9;$xt++) {$x[$xt]=($x[$xt]+$tx);} $y[1] = 250-375; $y[2] = 250-375; $y[3] = 500-375; $y[4] = 500-375; $y[5] = 250-375; $y[6] = 250-375; $y[7] = 500-375; $y[8] = 500-375; $y[9] = 500-500; # Y axis translation $ty=0; for ($yt=1;$yt<=9;$yt++) {$y[$yt]=($y[$yt]+$ty);} $z[1] = 250+100+250; $z[2] = 250+100+250; $z[3] = 250+100+250; $z[4] = 250+100+250; $z[5] = 500+100+250; $z[6] = 500+100+250; $z[7] = 500+100+250; $z[8] = 500+100+250; $z[9] = 500-25+250; # Y axis translation $tz=0; for ($zt=1;$zt<=9;$zt++) {$z[$zt]=($z[$zt]+$tz);} $xa=$x[1]; $ya=$y[1]; $za=$z[1]; #adjust focal length $fl = 100; # define degree to radians $rad= 3.1415926535/180; # currently - specify axis and angle of rotation $xr=0; $yr=0; $zr=0; # print "axis and rotation: \n"; # print "X = $xr "; # print "Y = $yr "; # print "Z = $zr "; # print "\n\n"; # print "\n\n"; for ($i=1;$i<=8;$i++) { #defines temp X Y Z coordinates for translation and rotation calculations $xa=$x[$i]; $ya=$y[$i]; $za=$z[$i]; #creates local coordinates around origin (0,0,0) $xa=$xa-$x[9]; $ya=$ya-$y[9]; $za=$za-$z[9]; # Rotational calculations # Pitch (X axis) $xb= $xa; $yb= $ya*cos($xr*$rad)-$za*sin($xr*$rad); $zb= $ya*sin($xr*$rad)+$za*cos($xr*$rad); #Yaw (Y axis) $xc = $zb*sin($yr*$rad)+$xb*cos($yr*$rad); $yc = $yb; $zc = $zb*cos($yr*$rad)-$xb*sin($yr*$rad); # Roll (Z axis) $xd = $xc*cos($zr*$rad)-$yc*sin($zr*$rad); $yd = $xc*sin($zr*$rad)+$yc*cos($zr*$rad); $zd = $zc; # re-introduces translated coordinates $xf[$i]=$xd+$x[9]; $yf[$i]=$yd+$y[9]; $zf[$i]=$zd+$z[9]; } # calculate shadow #################### #################### #introduce single light coordinates $lx = -300; $ly = -920; $lz = 150; #designate floor plane $fly = 400; $yra = $ly - $fly; for ($i=1;$i<=8;$i++) { $yrs = $ly - $yf[$i]; $xrs = $lx - $xf[$i]; $zrs = $lz - $zf[$i]; $yxrat = $yrs/$xrs; $yzrat = $yrs/$zrs; #remember a/A = b/B and a/b = A/B (where a is height and b is horizontal component) # $yxrat = a/b in width # $yzrat = a/b in depth # $yra = A # B in width = $lx-$shx[$i] # B in depth = $lz-$shz[$i] $shx[$i] = $yra/$yxrat; $shx[$i] = $shx[$i]-$lx; $shx[$i] = $shx[$i]*-1; $shz[$i] = $yra/$yzrat; $shz[$i] = $shz[$i]-$lz; $shz[$i] = $shz[$i]*-1; # print "z $shz[$i] x $shx[$i]\n"; } ##calculate 2d coordinates of shadow on ground plane for ($i=1;$i<=8;$i++) {$us[$i] = $shx[$i] / $shz[$i] * $fl;} for ($i=1;$i<=8;$i++) {$vs[$i] = $fly / $shz[$i] * $fl;} for ($i=1;$i<=8;$i++) { # print "u $us[$i] v $vs[$i]\n"; } for ($i=1;$i<=8;$i++) { $shu[$i] = ($us[$i]*4)+360; $shv[$i] = ($vs[$i]*4)+240; # print "u $shu[$i] v $shv[$i]\n"; } # designate and calculate ground plane #$fly = 400; # 16 X and 16 Z values $fgx = 900; $gx[1] = $fgx*-1; $gx[2] = $fgx*-1; $gx[3] = $fgx/-3; $gx[4] = $fgx/-3; $gx[5] = $fgx/3; $gx[6] = $fgx/3; $gx[7] = $fgx; $gx[8] = $fgx; $gx[9] = $fgx*-1; $gx[10] = $fgx; $gx[11] = $fgx*-1; $gx[12] = $fgx; $gx[13] = $fgx*-1; $gx[14] = $fgx; $gx[15] = $fgx*-1; $gx[16] = $fgx; $gz[1] = 150; $gz[2] = 2800; $gz[3] = 150; $gz[4] = 2800; $gz[5] = 150; $gz[6] = 2800; $gz[7] = 150; $gz[8] = 2800; $gz[9] = 150; $gz[10] = 150; $gz[11] = 1007; $gz[12] = 1007; $gz[13] = 1924; $gz[14] = 1924; $gz[15] = 2800; $gz[16] = 2800; for ($i=1;$i<=16;$i++) {$ug[$i] = $gx[$i] / $gz[$i] * $fl;} for ($i=1;$i<=16;$i++) {$vg[$i] = $fly / $gz[$i] * $fl;} for ($i=1;$i<=16;$i++) { $sgu[$i] = ($ug[$i]*4)+360; $sgv[$i] = ($vg[$i]*4)+240; # print "$ug[$i] $vg[$i]\n"; # print "$gx[$i] $gz[$i]\n"; # print "$sgu[$i] $sgv[$i]\n"; } # calculate 2d coordinates for ($i=1;$i<=8;$i++) {$un[$i] = $xf[$i] / $zf[$i] * $fl;} for ($i=1;$i<=8;$i++) {$vn[$i] = $yf[$i] / $zf[$i] * $fl;} for ($i=1;$i<=8;$i++) { $scu[$i] = ($un[$i]*4)+360; $scv[$i] = ($vn[$i]*4)+240; } $color = '#AAAAAAAAAAAA'; $bg = '#11118888CCCC'; $shadow = '#111111111111'; my $mw = MainWindow->new; $mw->title ("Cube with rotation x= $xr y= $yr z= $zr"); my $canvas = $mw->Canvas(-width => 720, -height => 600,-background => $bg)->grid; $mw->waitVisibility; $bttna = $canvas->Button(-text => " + X rot ", -command => sub { &x_rcalp }); $id = $canvas->createWindow(34, 555, -window => $bttna); $bttnb = $canvas->Button(-text => " + Y rot ", -command => sub { &y_rcalp }); $id = $canvas->createWindow(115, 555, -window => $bttnb); $bttnc = $canvas->Button(-text => " + Z rot ", -command => sub { &z_rcalp }); $id = $canvas->createWindow(200, 555, -window => $bttnc); $bttnd = $canvas->Button(-text => " + X trans ", -command => sub { &x_tcalp }); $id = $canvas->createWindow(285, 555, -window => $bttnd); $bttne = $canvas->Button(-text => " + Y trans ", -command => sub { &y_tcalp }); $id = $canvas->createWindow(370, 555, -window => $bttne); $bttnf = $canvas->Button(-text => " + Z trans ", -command => sub { &z_tcalp }); $id = $canvas->createWindow(455, 555, -window => $bttnf); $bttn = $canvas->Button(-text => " Exit ", -command => sub { exit }); $id = $canvas->createWindow(695, 585, -window => $bttn); $bttn1 = $canvas->Button(-text => " - X rot ", -command => sub { &x_rcalm }); $id = $canvas->createWindow(34, 585, -window => $bttn1); $bttn2 = $canvas->Button(-text => " - Y rot ", -command => sub { &y_rcalm }); $id = $canvas->createWindow(115, 585, -window => $bttn2); $bttn3 = $canvas->Button(-text => " - Z rot ", -command => sub { &z_rcalm }); $id = $canvas->createWindow(200, 585, -window => $bttn3); $bttn4 = $canvas->Button(-text => " - X trans ", -command => sub { &x_tcalm }); $id = $canvas->createWindow(285, 585, -window => $bttn4); $bttn5 = $canvas->Button(-text => " - Y trans ", -command => sub { &y_tcalm }); $id = $canvas->createWindow(370, 585, -window => $bttn5); $bttn6 = $canvas->Button(-text => " - Z trans ", -command => sub { &z_tcalm }); $id = $canvas->createWindow(455, 585, -window => $bttn6); # draw ground $canvas->createLine ($sgu[1], $sgv[1], $sgu[2], $sgv[2], -fill => $color); $canvas->createLine ($sgu[5], $sgv[5], $sgu[6], $sgv[6], -fill => $color); $canvas->createLine ($sgu[3], $sgv[3], $sgu[4], $sgv[4], -fill => $color); $canvas->createLine ($sgu[7], $sgv[7], $sgu[8], $sgv[8], -fill => $color); $canvas->createLine ($sgu[9], $sgv[9], $sgu[10], $sgv[10], -fill => $color); $canvas->createLine ($sgu[11], $sgv[11], $sgu[12], $sgv[12], -fill => $color); # $canvas->createLine ($sgu[13], $sgv[13], $sgu[14], $sgv[14], -fill => $color); # $canvas->createLine ($sgu[15], $sgv[15], $sgu[16], $sgv[16], -fill => $color); $canvas->createLine (0, $sgv[13], 720, $sgv[14], -fill => $color); $canvas->createLine (0, $sgv[15], 720, $sgv[16], -fill => $color); # draw shadow # 6 solid polygons # # 1 2 4 3 f # 6 5 7 8 b # 5 1 3 7 l # 2 6 8 4 r # 1 5 6 2 t # 3 4 8 7 u #ex $canvas->createPolygon ($shu[1], $shv[1], $shu[2], $shv[2], # $shu[4], $shv[4], $shu[3], $shv[3], -fill => $shadow); $canvas->createPolygon ($shu[1], $shv[1], $shu[2], $shv[2], $shu[4], $shv[4], $shu[3], $shv[3], -fill => $shadow); $canvas->createPolygon ($shu[6], $shv[6], $shu[5], $shv[5], $shu[7], $shv[7], $shu[8], $shv[8], -fill => $shadow); $canvas->createPolygon ($shu[5], $shv[5], $shu[1], $shv[1], $shu[3], $shv[3], $shu[7], $shv[7], -fill => $shadow); $canvas->createPolygon ($shu[2], $shv[2], $shu[6], $shv[6], $shu[8], $shv[8], $shu[4], $shv[4], -fill => $shadow); $canvas->createPolygon ($shu[1], $shv[1], $shu[5], $shv[5], $shu[6], $shv[6], $shu[2], $shv[2], -fill => $shadow); $canvas->createPolygon ($shu[3], $shv[3], $shu[4], $shv[4], $shu[8], $shv[8], $shu[7], $shv[7], -fill => $shadow); #draw cube $canvas->createPolygon ($scu[1], $scv[1], $scu[2], $scv[2], $scu[4], $scv[4], $scu[3], $scv[3], -fill => 'magenta'); # F ########################## #need Z- depth calculator ########################## ###corner connections # 1 = F L T # 2 = F R T # 3 = F L U # 4 = F R U # 5 = B T L # 6 = B T R # 7 = B L U # 8 = B R U for ($i=1;$i<=8;$i++) { $absxf[$i]=$xf[$i]; $absyf[$i]=$yf[$i]; $abszf[$i]=$zf[$i]; if (($absxf[$i])<0) {$absxf[$i]=$absxf[$i]*-1;} if (($absyf[$i])<0) {$absyf[$i]=$absyf[$i]*-1;} if (($abszf[$i])<0) {$abszf[$i]=$abszf[$i]*-1;} } for ($i=1;$i<=8;$i++) {$depzz[$i] = $absxf[$i] + $absyf[$i] + $abszf[$i];} for ($i=1;$i<=8;$i++) {print "$absxf[$i] $absyf[$i] $abszf[$i]\n";} for ($i=1;$i<=8;$i++) {print "$depzz[$i]\n";} #z depth of faces #Back $face[1] = "Back"; $dpz[1] = ($depzz[6] + $depzz[5] + $depzz[7] + $depzz[8]); #Left $face[2] = "Left"; $dpz[2] = ($depzz[1] + $depzz[5] + $depzz[7] + $depzz[3]); #Right $face[3] = "Rght"; $dpz[3] = ($depzz[6] + $depzz[2] + $depzz[4] + $depzz[8]); #Top $face[4] = "Top "; $dpz[4] = ($depzz[6] + $depzz[2] + $depzz[1] + $depzz[5]); #Underside $face[5] = "Undr"; $dpz[5] = ($depzz[3] + $depzz[7] + $depzz[4] + $depzz[8]); #Front $face[6] = "Frnt"; $dpz[6] = ($depzz[1] + $depzz[2] + $depzz[4] + $depzz[3]); ##### #sort $dpz{i} for ($i=1;$i<=6;$i++) { # print "$face[$i] $dpz[$i]\n"; } MainLoop; sub x_rcalp { print "x_rcalp\n"; $xr=$xr + 5; $yr=$yr+0; $zr=$zr+0; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub y_rcalp { print "y_rcalp\n"; $xr=$xr+0; $yr=$yr + 5; $zr=$zr+0; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub z_rcalp { print "z_rcalp\n"; $xr=$xr+0; $yr=$yr+0; $zr=$zr + 5; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub x_tcalp { print "x_tcalp\n"; $tx = 5; $ty = 0; $tz = 0; &re_calc; } sub y_tcalp { print "y_tcalp\n"; $tx = 0; $ty = -5; $tz = 0; &re_calc; } sub z_tcalp { print "z_tcalp\n"; $tx = 0; $ty = 0; $tz = 5; &re_calc; } sub x_rcalm { print "x_rcalm\n"; $xr=$xr - 5; $yr=$yr+0; $zr=$zr+0; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub y_rcalm { print "y_rcalm\n"; $xr=$xr+0; $yr=$yr -5; $zr=$zr+0; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub z_rcalm { print "z_rcalm\n"; $xr=$xr+0; $yr=$yr+0; $zr=$zr-5; $tx = 0; $ty = 0; $tz = 0; &re_calc; } sub x_tcalm { print "x_tcalm\n"; $tx = - 5; $ty = 0; $tz = 0; &re_calc; } sub y_tcalm { print "y_tcalm\n"; $tx = 0; $ty = + 5; $tz = 0; print "$yf[$i]\n"; # if any y value is less then or equak to 0, then $ty = 0 for ($i=1;$i<=8;$i++) { if ($yf[$i]>=400) {$ty = 0;} } &re_calc; } sub z_tcalm { print "z_tcalm\n"; $tx = 0; $ty = 0; $tz = - 5; &re_calc; } sub re_calc { for ($t=1;$t<=9;$t++) {$x[$t]=($x[$t]+$tx);} for ($t=1;$t<=9;$t++) {$y[$t]=($y[$t]+$ty);} for ($t=1;$t<=9;$t++) {$z[$t]=($z[$t]+$tz);} print "$tx $ty $tz\n"; print "$xr $yr $zr\n"; for ($i=1;$i<=8;$i++) { #defines temp X Y Z coordinates for translation and rotation calculations $xa=$x[$i]; $ya=$y[$i]; $za=$z[$i]; #creates local coordinates around origin (0,0,0) # print "$x[9] $y[9] $z[9]\n"; $xa=$xa-$x[9]; $ya=$ya-$y[9]; $za=$za-$z[9]; # Rotational calculations # Pitch (X axis) $xb= $xa; $yb= $ya*cos($xr*$rad)-$za*sin($xr*$rad); $zb= $ya*sin($xr*$rad)+$za*cos($xr*$rad); #Yaw (Y axis) $xc = $zb*sin($yr*$rad)+$xb*cos($yr*$rad); $yc = $yb; $zc = $zb*cos($yr*$rad)-$xb*sin($yr*$rad); # Roll (Z axis) $xd = $xc*cos($zr*$rad)-$yc*sin($zr*$rad); $yd = $xc*sin($zr*$rad)+$yc*cos($zr*$rad); $zd = $zc; # re-introduces translated coordinates $xf[$i]=$xd+$x[9]; $yf[$i]=$yd+$y[9]; $zf[$i]=$zd+$z[9]; } # calculate 2d coordinates for ($i=1;$i<=8;$i++) {$un[$i] = $xf[$i] / $zf[$i] * $fl;} for ($i=1;$i<=8;$i++) {$vn[$i] = $yf[$i] / $zf[$i] * $fl;} for ($i=1;$i<=8;$i++) { $tcu[$i] = $scu[$i]; $tcv[$i] = $scv[$i]; $scu[$i] = ($un[$i]*4)+360; $scv[$i] = ($vn[$i]*4)+240; } for ($i=1;$i<=8;$i++) { # print "$scu[$i] "; # print "$scv[$i] "; } ################# for ($i=1;$i<=8;$i++) { $yrs = $ly - $yf[$i]; $xrs = $lx - $xf[$i]; $zrs = $lz - $zf[$i]; $yxrat = $yrs/$xrs; $yzrat = $yrs/$zrs; #remember a/A = b/B and a/b = A/B (where a is height and b is horizontal component) # $yxrat = a/b in width # $yzrat = a/b in depth # $yra = A # B in width = $lx-$shx[$i] # B in depth = $lz-$shz[$i] $shx[$i] = $yra/$yxrat; $shx[$i] = $shx[$i]-$lx; $shx[$i] = $shx[$i]*-1; $shz[$i] = $yra/$yzrat; $shz[$i] = $shz[$i]-$lz; $shz[$i] = $shz[$i]*-1; # print "z $shz[$i] x $shx[$i]\n"; } ##calculate 2d coordinates of shadow on ground plane for ($i=1;$i<=8;$i++) {$us[$i] = $shx[$i] / $shz[$i] * $fl;} for ($i=1;$i<=8;$i++) {$vs[$i] = $fly / $shz[$i] * $fl;} for ($i=1;$i<=8;$i++) { # print "u $us[$i] v $vs[$i]\n"; } for ($i=1;$i<=8;$i++) { $thu[$i] = $shu[$i]; $thv[$i] = $shv[$i]; $shu[$i] = ($us[$i]*4)+360; $shv[$i] = ($vs[$i]*4)+240; # print "u $shu[$i] v $shv[$i]\n"; } # erase old shadow $canvas->createLine ($thu[1], $thv[1], $thu[2], $thv[2], -fill => $bg, -width =>20); $canvas->createLine ($thu[2], $thv[2], $thu[4], $thv[4], -fill => $bg, -width =>20); $canvas->createLine ($thu[3], $thv[3], $thu[4], $thv[4], -fill => $bg, -width =>20); $canvas->createLine ($thu[3], $thv[3], $thu[1], $thv[1], -fill => $bg, -width =>20); $canvas->createLine ($thu[5], $thv[5], $thu[6], $thv[6], -fill => $bg, -width =>20); $canvas->createLine ($thu[6], $thv[6], $thu[8], $thv[8], -fill => $bg, -width =>20); $canvas->createLine ($thu[7], $thv[7], $thu[8], $thv[8], -fill => $bg, -width =>20); $canvas->createLine ($thu[7], $thv[7], $thu[5], $thv[5], -fill => $bg, -width =>20); $canvas->createLine ($thu[5], $thv[5], $thu[1], $thv[1], -fill => $bg, -width =>20); $canvas->createLine ($thu[6], $thv[6], $thu[2], $thv[2], -fill => $bg, -width =>20); $canvas->createLine ($thu[7], $thv[7], $thu[3], $thv[3], -fill => $bg, -width =>20); $canvas->createLine ($thu[8], $thv[8], $thu[4], $thv[4], -fill => $bg, -width =>20); #erase old cube # $canvas->createLine ($tcu[1], $tcv[1], $tcu[2], $tcv[2], -fill => $bg); # $canvas->createLine ($tcu[2], $tcv[2], $tcu[4], $tcv[4], -fill => $bg); # $canvas->createLine ($tcu[3], $tcv[3], $tcu[4], $tcv[4], -fill => $bg); # $canvas->createLine ($tcu[3], $tcv[3], $tcu[1], $tcv[1], -fill => $bg); # $canvas->createLine ($tcu[5], $tcv[5], $tcu[6], $tcv[6], -fill => $bg); # $canvas->createLine ($tcu[6], $tcv[6], $tcu[8], $tcv[8], -fill => $bg); # $canvas->createLine ($tcu[7], $tcv[7], $tcu[8], $tcv[8], -fill => $bg); # $canvas->createLine ($tcu[7], $tcv[7], $tcu[5], $tcv[5], -fill => $bg); # $canvas->createLine ($tcu[5], $tcv[5], $tcu[1], $tcv[1], -fill => $bg); # $canvas->createLine ($tcu[6], $tcv[6], $tcu[2], $tcv[2], -fill => $bg); # $canvas->createLine ($tcu[7], $tcv[7], $tcu[3], $tcv[3], -fill => $bg); # $canvas->createLine ($tcu[8], $tcv[8], $tcu[4], $tcv[4], -fill => $bg); $canvas->createPolygon ($tcu[1], $tcv[1], $tcu[2], $tcv[2], $tcu[4], $tcv[4], $tcu[3], $tcv[3], -fill => $bg); $canvas->createPolygon ($tcu[6], $tcv[6], $tcu[5], $tcv[5], $tcu[7], $tcv[7], $tcu[8], $tcv[8], -fill => $bg); $canvas->createPolygon ($tcu[5], $tcv[5], $tcu[1], $tcv[1], $tcu[3], $tcv[3], $tcu[7], $tcv[7], -fill => $bg); $canvas->createPolygon ($tcu[2], $tcv[2], $tcu[6], $tcv[6], $tcu[8], $tcv[8], $tcu[4], $tcv[4], -fill => $bg); $canvas->createPolygon ($tcu[1], $tcv[1], $tcu[5], $tcv[5], $tcu[6], $tcv[6], $tcu[2], $tcv[2], -fill => $bg); $canvas->createPolygon ($tcu[3], $tcv[3], $tcu[4], $tcv[4], $tcu[8], $tcv[8], $tcu[7], $tcv[7], -fill => $bg); # draw ground $canvas->createLine ($sgu[1], $sgv[1], $sgu[2], $sgv[2], -fill => $color); $canvas->createLine ($sgu[5], $sgv[5], $sgu[6], $sgv[6], -fill => $color); $canvas->createLine ($sgu[3], $sgv[3], $sgu[4], $sgv[4], -fill => $color); $canvas->createLine ($sgu[7], $sgv[7], $sgu[8], $sgv[8], -fill => $color); $canvas->createLine ($sgu[9], $sgv[9], $sgu[10], $sgv[10], -fill => $color); $canvas->createLine ($sgu[11], $sgv[11], $sgu[12], $sgv[12], -fill => $color); # $canvas->createLine ($sgu[13], $sgv[13], $sgu[14], $sgv[14], -fill => $color); # $canvas->createLine ($sgu[15], $sgv[15], $sgu[16], $sgv[16], -fill => $color); # X 0 to X 720 creates horizon line $canvas->createLine (0, $sgv[13], 720, $sgv[14], -fill => $color); $canvas->createLine (0, $sgv[15], 720, $sgv[16], -fill => $color); # draw shadow # draw shadow # 6 solid polygons # # 1 2 4 3 f # 6 5 7 8 b # 5 1 3 7 l # 2 6 8 4 r # 1 5 6 2 t # 3 4 8 7 u #ex $canvas->createPolygon ($shu[1], $shv[1], $shu[2], $shv[2], # $shu[4], $shv[4], $shu[3], $shv[3], -fill => $shadow); $canvas->createPolygon ($shu[1], $shv[1], $shu[2], $shv[2], $shu[4], $shv[4], $shu[3], $shv[3], -fill => $shadow); $canvas->createPolygon ($shu[6], $shv[6], $shu[5], $shv[5], $shu[7], $shv[7], $shu[8], $shv[8], -fill => $shadow); $canvas->createPolygon ($shu[5], $shv[5], $shu[1], $shv[1], $shu[3], $shv[3], $shu[7], $shv[7], -fill => $shadow); $canvas->createPolygon ($shu[2], $shv[2], $shu[6], $shv[6], $shu[8], $shv[8], $shu[4], $shv[4], -fill => $shadow); $canvas->createPolygon ($shu[1], $shv[1], $shu[5], $shv[5], $shu[6], $shv[6], $shu[2], $shv[2], -fill => $shadow); $canvas->createPolygon ($shu[3], $shv[3], $shu[4], $shv[4], $shu[8], $shv[8], $shu[7], $shv[7], -fill => $shadow); #draw new cube ########################## #need Z- depth calculator ########################## #corner connections # 1 = F L T # 2 = F R T # 3 = F L U # 4 = F R U # 5 = B T L # 6 = B T R # 7 = B L U # 8 = B R U for ($i=1;$i<=8;$i++) { $absxf[$i]=$xf[$i]; $absyf[$i]=$yf[$i]; $abszf[$i]=$zf[$i]; if (($absxf[$i])<0) {$absxf[$i]=$absxf[$i]*-1;} if (($absyf[$i])<0) {$absyf[$i]=$absyf[$i]*-1;} if (($abszf[$i])<0) {$abszf[$i]=$abszf[$i]*-1;} } for ($i=1;$i<=8;$i++) {$depzz[$i] = $absxf[$i] + $absyf[$i] + $abszf[$i] - $fl;} for ($i=1;$i<=8;$i++) {$depzz[$i] = $absxf[$i] + $absyf[$i] + $abszf[$i];} for ($i=1;$i<=8;$i++) {print "$absxf[$i] $absyf[$i] $abszf[$i]\n";} for ($i=1;$i<=8;$i++) {print "$depzz[$i]\n";} #z depth of faces #Back $face[1] = "Back"; $dpz[1] = ($depzz[6] + $depzz[5] + $depzz[7] + $depzz[8]); #Left $face[2] = "Left"; $dpz[2] = ($depzz[1] + $depzz[5] + $depzz[7] + $depzz[3]); #Right $face[3] = "Rght"; $dpz[3] = ($depzz[6] + $depzz[2] + $depzz[4] + $depzz[8]); #Top $face[4] = "Top "; $dpz[4] = ($depzz[6] + $depzz[2] + $depzz[1] + $depzz[5]); #Underside $face[5] = "Undr"; $dpz[5] = ($depzz[3] + $depzz[7] + $depzz[4] + $depzz[8]); #Front $face[6] = "Frnt"; $dpz[6] = ($depzz[1] + $depzz[2] + $depzz[4] + $depzz[3]); #################################################################### #################################################################### #################################################################### ##### #sort $dpz{i} #for ($i=1;$i<=6;$i++) {$dpz[$i]=$dpz[$i]*100;} #for ($i=1;$i<=6;$i++) {$dpz[$i]=int($dpz[$i]/100);} #for ($i=1;$i<=6;$i++) { # print "$face[$i] $dpz[$i]\n"; #} for ($i=1;$i<=6;$i++) {$sordpz[$i] = $dpz[$i];} for ($i=1;$i<=6;$i++) { for ($j=1;$j<=5;$j++) { if ($sordpz[$j+1]<$sordpz[$j]) { $tordpz = $sordpz[$j]; $sordpz[$j] = $sordpz[$j+1]; $sordpz[$j+1] = $tordpz; } } } # for ($i=1;$i<=6;$i++) {print "$sordpz[$i]\n";} # print "\n\n"; # print "$sordpz[3]\n"; # print "$sordpz[2]\n"; # print "$sordpz[1]\n"; #for ($i=1;$i<=6;$i++) { print "face $face[$i] depth $dpz[$i]\n"; #} if ($sordpz[4]== $dpz[1]) { $canvas->createPolygon ($scu[6], $scv[6], $scu[5], $scv[5], $scu[7], $scv[7], $scu[8], $scv[8], -fill => 'red'); # B } if ($sordpz[4]== $dpz[2]) { $canvas->createPolygon ($scu[5], $scv[5], $scu[1], $scv[1], $scu[3], $scv[3], $scu[7], $scv[7], -fill => 'green'); # L } if ($sordpz[4]== $dpz[3]) { $canvas->createPolygon ($scu[2], $scv[2], $scu[6], $scv[6], $scu[8], $scv[8], $scu[4], $scv[4], -fill => 'blue'); # R } if ($sordpz[4]== $dpz[4]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[5], $scv[5], $scu[6], $scv[6], $scu[2], $scv[2], -fill => 'cyan'); # T } if ($sordpz[4]== $dpz[5]) { $canvas->createPolygon ($scu[3], $scv[3], $scu[4], $scv[4], $scu[8], $scv[8], $scu[7], $scv[7], -fill => 'yellow'); # U } if ($sordpz[4]== $dpz[6]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[2], $scv[2], $scu[4], $scv[4], $scu[3], $scv[3], -fill => 'magenta'); # F } if ($sordpz[3]== $dpz[1]) { $canvas->createPolygon ($scu[6], $scv[6], $scu[5], $scv[5], $scu[7], $scv[7], $scu[8], $scv[8], -fill => 'red'); # B } if ($sordpz[3]== $dpz[2]) { $canvas->createPolygon ($scu[5], $scv[5], $scu[1], $scv[1], $scu[3], $scv[3], $scu[7], $scv[7], -fill => 'green'); # L } if ($sordpz[3]== $dpz[3]) { $canvas->createPolygon ($scu[2], $scv[2], $scu[6], $scv[6], $scu[8], $scv[8], $scu[4], $scv[4], -fill => 'blue'); # R } if ($sordpz[3]== $dpz[4]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[5], $scv[5], $scu[6], $scv[6], $scu[2], $scv[2], -fill => 'cyan'); # T } if ($sordpz[3]== $dpz[5]) { $canvas->createPolygon ($scu[3], $scv[3], $scu[4], $scv[4], $scu[8], $scv[8], $scu[7], $scv[7], -fill => 'yellow'); # U } if ($sordpz[3]== $dpz[6]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[2], $scv[2], $scu[4], $scv[4], $scu[3], $scv[3], -fill => 'magenta'); # F } if ($sordpz[2]== $dpz[1]) { $canvas->createPolygon ($scu[6], $scv[6], $scu[5], $scv[5], $scu[7], $scv[7], $scu[8], $scv[8], -fill => 'red'); # B } if ($sordpz[2]== $dpz[2]) { $canvas->createPolygon ($scu[5], $scv[5], $scu[1], $scv[1], $scu[3], $scv[3], $scu[7], $scv[7], -fill => 'green'); # L } if ($sordpz[2]== $dpz[3]) { $canvas->createPolygon ($scu[2], $scv[2], $scu[6], $scv[6], $scu[8], $scv[8], $scu[4], $scv[4], -fill => 'blue'); # R } if ($sordpz[2]== $dpz[4]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[5], $scv[5], $scu[6], $scv[6], $scu[2], $scv[2], -fill => 'cyan'); # T } if ($sordpz[2]== $dpz[5]) { $canvas->createPolygon ($scu[3], $scv[3], $scu[4], $scv[4], $scu[8], $scv[8], $scu[7], $scv[7], -fill => 'yellow'); # U } if ($sordpz[2]== $dpz[6]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[2], $scv[2], $scu[4], $scv[4], $scu[3], $scv[3], -fill => 'magenta'); # F } if ($sordpz[1]== $dpz[1]) { $canvas->createPolygon ($scu[6], $scv[6], $scu[5], $scv[5], $scu[7], $scv[7], $scu[8], $scv[8], -fill => 'red'); # B } if ($sordpz[1]== $dpz[2]) { $canvas->createPolygon ($scu[5], $scv[5], $scu[1], $scv[1], $scu[3], $scv[3], $scu[7], $scv[7], -fill => 'green'); # L } if ($sordpz[1]== $dpz[3]) { $canvas->createPolygon ($scu[2], $scv[2], $scu[6], $scv[6], $scu[8], $scv[8], $scu[4], $scv[4], -fill => 'blue'); # R } if ($sordpz[1]== $dpz[4]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[5], $scv[5], $scu[6], $scv[6], $scu[2], $scv[2], -fill => 'cyan'); # T } if ($sordpz[1]== $dpz[5]) { $canvas->createPolygon ($scu[3], $scv[3], $scu[4], $scv[4], $scu[8], $scv[8], $scu[7], $scv[7], -fill => 'yellow'); # U } if ($sordpz[1]== $dpz[6]) { $canvas->createPolygon ($scu[1], $scv[1], $scu[2], $scv[2], $scu[4], $scv[4], $scu[3], $scv[3], -fill => 'magenta'); # F } }