POV-Ray : Newsgroups : povray.advanced-users : Calculating slope of a line : Re: Calculating slope of a line Server Time
1 Jul 2024 05:15:41 EDT (-0400)
  Re: Calculating slope of a line  
From: Kene
Date: 13 Mar 2010 17:50:01
Message: <web.4b9c162a7ca26fad772dd76f0@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
>
> Maybe we can see actual code ?
>

Below is the actual macro. Basically all variables are vectors and y is the
height so remains the same for all variables.


#macro ConnRSide( SName, SRef1, Ref2, Thickness, Angle, Sender )
  // Get object name and snap Id
  Get_SnapId( SName, 1 )
  #local Obj_Name      = Found_Object_Name;
  #local Obj_Snap_Name = Found_Snap_Name;
  // find object name
  Find_Object( Obj_Name )
  #local Obj_Index = Found_Object_Index;
  // get ref1 name
  Get_SnapId( SRef1, 1 )
  #local Ref1_Name      = Found_Object_Name;
  #local Ref1_Snap_Name = Found_Snap_Name;
  // find ref1 and Change angle according to sender
  Find_Object( Ref1_Name )
  #local Ref1_Index = Found_Object_Index;
  #local Ref1_Type  = kobldes_Objects_Id[ Ref1_Index ][1];
  #local Ref1_Angle = kobldes_Objects_Pt[ Ref1_Index ][0][2];
  // before we manipulate the angle, let get a second point along the new line
  CalcCurvePoint( Angle, 1000 )
  #local AngleL = SnapCoords[0];
  #if( strcmp( Sender, "RConnRSide" )=0 )
    #local Angle = Angle + Ref1_Angle;
  #end
  // get ref1 coords
  Get_Snap_Location( Ref1_Snap_Name, Ref1_Index, Ref1_Name )
  #local Ref1_Pos = Found_Snap_Location + kobldes_Objects_Pt[ Ref1_Index
][1][1];
  // get ref2 name
  Get_SnapId( Ref2, 1 )
  #local Ref2_Name      = Found_Object_Name;
  #local Ref2_Snap_Name = Found_Snap_Name;
  #local Ref2_Snap1     = concat( Ref2_Snap_Name, "1" );
  // find ref2
  Find_Object( Ref2_Name )
  #local Ref2_Index = Found_Object_Index;
  #local Ref2_Type  = kobldes_Objects_Id[ Ref2_Index ][1];
  #local Ref2_Angle = kobldes_Objects_Pt[ Ref2_Index ][0][2];
  Get_Snap_Location( Ref2_Snap1, Ref2_Index, Ref2_Name )
  #local Ref2_Snap1_Pos  = Found_Snap_Location + kobldes_Objects_Pt[ Ref2_Index
][1][1];
  //nDebug( "Ref2"
  // test if the angle of ref2 is 0/180/360 or 90/270
  // use switch
  #if( Ref2_Angle = 0 | Ref2_Angle = 180 | Ref2_Angle = 360 )
    // here we know that the slope is undefined
    // In the following calculations, y is z
    // 1. Horizontal distance: Ref2 - Ref1
    // 2. Length of new side: hyp = adj/cos Angle
    // 3. Make side
    // 4. Place it
    //#local HorLength = Ref2_Snap1_Pos.x - Ref1_Pos.x;          //1.
    #local HorLength = vlength( Ref2_Snap1_Pos - Ref1_Pos );     //1.
    #local SideLength = HorLength / cos( radians( 90 - Angle )); //2.
    RSide( Obj_Name, SideLength, Thickness, Angle )              //3.
    // place the side in the scene
    SOffset( SName, SRef1, 0,0,0 )                               //4.
  #else
    // In the following calculations, y is z
    // 1. Find a point from SRef1 to 1000 at the provided angle
    // 2. Find the slope of line from 1.
    // 3. Get start and mid-point of Ref2
    // 4. Find the slope of line from 3.
    // 5. Pick a point in line from 1. (x,y), find the y-intercept (b): y - mx =
b
    // 6. Pick a point in line from 3. (x,y), find the y-intercept (b): y - mx =
b
    // 7. find the common x point in both lines: x = (5. - 6.)/(4. + 2.)
    // 8. find the common y point in both lines: y = 2. * 7. + 5. (or y = 4. *
7. + 6.)
    #local AngleL = AngleL + kobldes_Objects_Pt[ Ref1_Index ][1][1]; //1.
    nDebug( "Calculated X length of line", AngleL.x )
    nDebug( "Calculated Y length of line", AngleL.y )
    nDebug( "Calculated Z length of line", AngleL.z )
    nDebug( "X Origin of new line", Ref1_Pos.x )
    nDebug( "Y Origin of new line", Ref1_Pos.y )
    nDebug( "Z Origin of new line", Ref1_Pos.z )
    #local SlopeL1 = (( AngleL.z ) - ( Ref1_Pos.z ))/(( AngleL.x )-( Ref1_Pos.x
));    //2.
    nDebug( "SlopeL1", SlopeL1 )
    // 3.
    #local Ref2_Snap2     = concat( Ref2_Snap_Name, "2" );
    Get_Snap_Location( Ref2_Snap2, Ref2_Index, Ref2_Name )
    #local Ref2_Snap2_Pos  = Found_Snap_Location + kobldes_Objects_Pt[
Ref2_Index ][1][1];
    #local SlopeL2 = ( Ref2_Snap2_Pos.z - Ref2_Snap1_Pos.z )/( Ref2_Snap2_Pos.x
-
      Ref2_Snap1_Pos.x );                                                  //4.
    #local BL1 = AngleL.z - ( SlopeL1 * AngleL.x );
//5.
    #local BL2 = Ref2_Snap2_Pos.z - ( SlopeL2 * Ref2_Snap2_Pos.x );        //6.
    #local CommonX = ( BL1 - BL2 )/( SlopeL2 + SlopeL1 );                  //7.
    #local CommonYa = SlopeL1 * CommonX + BL1;                             //8a.
    #local CommonYb = SlopeL2 * CommonX + BL2;                             //8b
    #if( CommonYa = CommonYb )
      //create new side here
      #local SideXYZ = < CommonX, 0, CommonYa >;
      Find_Length_Angle( SideXYZ, Ref1_Pos ) // Y value is ignored in this
function
      #local SideLength = Found_Length;
      RSide( Obj_Name, SideLength, Thickness, Angle )
      // place the side in the scene
      SOffset( SName, SRef1, 0,0,0 )
    #else
      sDebug( "ConnRSide: New side does not intersect existing side", Obj_Name )
    #end
  #end


Post a reply to this message

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