POV-Ray : Newsgroups : povray.advanced-users : How to model a cube in 2D? : Re: How to model a cube in 2D? Server Time
29 Jul 2024 20:24:30 EDT (-0400)
  Re: How to model a cube in 2D?  
From: Tor Olav Kristensen
Date: 24 Jan 2001 21:22:26
Message: <3A6F8DA3.952D3A41@online.no>
Peter Popov wrote:
>...
> I have some trouble with the 3D calculations but I'll see if John's
> page will help me solve them.
>...
 
I don't know what your 3D-troubles are, but maybe 
the code below solves some of them.

Please ask if you want me to comment on the code.

Note that the code is made for POV's Left Handed System.

--
Best regards,

Tor Olav


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// Copyright 2001 by Tor Olav Kristensen
// mailto:tor### [at] hotmailcom
// http://www.crosswinds.net/~tok/tokrays.html
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#version 3.1;

#include "colors.inc"

global_settings { ambient_light color White }

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#macro vproject(v1, v2)
// Projection of the vector v1 in the direction of the vector v2.

  (v2*vdot(v1, v2)/vdot(v2, v2))

#end // macro vproject


#macro pt2pl(pPoint, pPlane, vPlane)
// Projection of a point to a plane

  (pPoint - vproject(pPoint - pPlane, vPlane))

#end // macro pt2pl


/*
#macro ln2pl(pLine, vLine, pPlane, vPlane)
// Intersection of a line and a plane

  (pLine + vLine*vdot(pPlane - pLine, vPlane)/vdot(vLine, vPlane))

#end // macro ln2pl
*/


#macro v2D3D(v2D, vBas1, vBas2)

  (v2D.u*vBas1 + v2D.v*vBas2)

#end // macro v2D3D


#macro v3D2D(v3D, vBas1, vBas2)

  <vdot(v3D, vBas1), vdot(v3D, vBas2)>

#end // macro v3D2D


#macro SphereSpread(CenterArray, Radius)

  #local Cnt = 0;
  #while (Cnt < dimension_size(CenterArray, 1))
    sphere { CenterArray[Cnt], Radius }
    #local Cnt = Cnt + 1;
  #end // while

#end // macro SphereSpread 


#macro CylinderSpread(EndsArray, ConnectArray, Radius)

  #local Cnt = 0;
  #while (Cnt < dimension_size(ConnectArray, 1))
    cylinder {
      EndsArray[ConnectArray[Cnt][0]],
      EndsArray[ConnectArray[Cnt][1]],
      Radius
    }
    #local Cnt = Cnt + 1;
  #end // while

#end // macro CylinderSpread


#macro CylindersBetween(EndsArray1, EndsArray2, Radius)

  #local Cnt = 0;
  #while (Cnt < dimension_size(EndsArray1, 1))
    cylinder {
      EndsArray1[Cnt],
      EndsArray2[Cnt],
      Radius
    }
    #local Cnt = Cnt + 1;
  #end // while

#end // macro CylindersBetween

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare NrOfCorners = 8;

#declare ArrayOfCorners =
array[NrOfCorners] {
  < 1, -1,  1>, <-1, -1,  1>, <-1, -1, -1>, < 1, -1, -1>, 
  < 1,  1,  1>, <-1,  1,  1>, <-1,  1, -1>, < 1,  1, -1>
}  


#declare NrOfEdges = 12;

#declare ArrayOfEdges =
array[NrOfEdges][2] {
  { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 },
  { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 },
  { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 }
}  

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare pAnyOffSet = <2, 1, -1>;
#declare vAnyRot = <30, 15, -20>;
#declare vAnyScale = <0.6, 1, 1.8>;

#declare Cnt = 0;
#while (Cnt < NrOfCorners)
  #declare ArrayOfCorners[Cnt] = ArrayOfCorners[Cnt] + pAnyOffSet;
  #declare ArrayOfCorners[Cnt] =  vrotate(ArrayOfCorners[Cnt], vAnyRot);
//  #declare ArrayOfCorners[Cnt] = ArrayOfCorners[Cnt]*vAnyScale;
  #declare Cnt = Cnt + 1;
#end // while

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

union {
  SphereSpread(ArrayOfCorners, 0.15)
  pigment { color Yellow }
}

union {
  CylinderSpread(ArrayOfCorners, ArrayOfEdges, 0.1)
  pigment { color Red }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare pPlane = -2*<1, 1, 1>;
#declare vPlane = <3, 1, 2>;

disc {
  pPlane, vPlane, 5
  pigment { color White }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare ArrayOfProjectedCorners = array[NrOfCorners]

#declare Cnt = 0;

#while (Cnt < NrOfCorners)
  #declare ArrayOfProjectedCorners[Cnt] =
    pt2pl(ArrayOfCorners[Cnt], pPlane, vPlane);
  #declare Cnt = Cnt + 1;
#end // while


/*
union {
  SphereSpread(ArrayOfProjectedCorners, 0.16)
  pigment { color White }
}

union {
  CylinderSpread(ArrayOfProjectedCorners, ArrayOfEdges, 0.11)
  pigment { color Gray }
}
*/

union {
  CylindersBetween(ArrayOfCorners, ArrayOfProjectedCorners, 0.05)
  pigment { color Orange }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare pOrigo2D = pt2pl(<0, 0, 0>, pPlane, vPlane);

sphere {
  pOrigo2D, 0.15
  pigment { color Magenta }
}

#declare vBasisU = vnormalize(vcross(vPlane, y));
#declare vBasisV = vnormalize(vcross(vBasisU, vPlane));

cylinder {
  pOrigo2D, pOrigo2D + 3*vBasisU, 0.1
  pigment { color Green }
}

cylinder {
  pOrigo2D, pOrigo2D + 3*vBasisV, 0.1
  pigment { color Green }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare ArrayOfCorners2D = array[NrOfCorners]

#declare Cnt = 0;
#while (Cnt < NrOfCorners)
  #declare ArrayOfCorners2D[Cnt] =
    v3D2D(ArrayOfProjectedCorners[Cnt] - pOrigo2D, vBasisU, vBasisV);
  #declare Cnt = Cnt + 1;
#end // while


#declare ArrayOfReconstructedCorners = array[NrOfCorners]

#declare Cnt = 0;
#while (Cnt < NrOfCorners)
  #declare ArrayOfReconstructedCorners[Cnt] =
    pOrigo2D + v2D3D(ArrayOfCorners2D[Cnt], vBasisU, vBasisV);
  #declare Cnt = Cnt + 1;
#end // while


union {
  SphereSpread(ArrayOfReconstructedCorners, 0.15)
  pigment { color Blue }
}

union {
  CylinderSpread(ArrayOfReconstructedCorners, ArrayOfEdges, 0.1)
  pigment { color Cyan }
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare vBasisU = x;
#declare vBasisV = y;

#declare ArrayOfNewCorners = array[NrOfCorners]

#declare Cnt = 0;
#while (Cnt < NrOfCorners)
  #declare ArrayOfNewCorners[Cnt] =
    v2D3D(ArrayOfCorners2D[Cnt], vBasisU, vBasisV);
  #declare Cnt = Cnt + 1;
#end // while

// Uncomment the other camera to see this
union {
  union {
    SphereSpread(ArrayOfNewCorners, 0.15)
    pigment { color White }
  }
  union {
    CylinderSpread(ArrayOfNewCorners, ArrayOfEdges, 0.1)
    pigment { color Yellow }
  }
  sphere {
    <0, 0, 0>, 0.15
    pigment { color Magenta }
  }
  cylinder {
    <0, 0, 0>, 3*vBasisU, 0.1
    pigment { color Green }
  }
  cylinder {
    <0, 0, 0>, 3*vBasisV, 0.1
    pigment { color Green }
  }
  translate 20*y
}

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

background { color Blue/2 }

light_source {
  100*<4, 2, -1>
  color White*1.5
  shadowless
}

camera {
  location <2, 1, -3>*2.5
  look_at pOrigo2D/2
}

/*
camera {
  location -8*vnormalize(vcross(vBasisU, vBasisV))
  look_at <0, 0, 0>
  translate 20*y
}

plane {
  -z, 0
  pigment { color Gray }
}
*/

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =


Post a reply to this message

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