POV-Ray : Newsgroups : povray.advanced-users : Calculating slope of a line Server Time
29 Oct 2024 22:45:13 EDT (-0400)
  Calculating slope of a line (Message 1 to 7 of 7)  
From: Kene
Subject: Calculating slope of a line
Date: 13 Mar 2010 16:10:01
Message: <web.4b9bfe2cc9c651bf772dd76f0@news.povray.org>
I am working on a math problem with POV-Ray where I am trying to calculate the
slope of a line. I have located two points on the line with the coordinates
(9000, -8022) and (-5000, -1792). Let us assume the first set of coordinates are
identified as Ref1_Pos.x, Ref1_Pos.z while the second set are Ref2_Pos.x and
Ref2_Pos.z. The slope formula will be:

Slope = ( Ref1_Pos.z - Ref2_Pos.z )/( Ref1_Pos.x - Ref2_Pos.x )

In manual calculation the above problem gives the result: -0.304862
With POV-Ray I get: -0

I know I am doing something wrong but I cannot figure it out. Please help.


Post a reply to this message

From: Le Forgeron
Subject: Re: Calculating slope of a line
Date: 13 Mar 2010 17:00:19
Message: <4b9c0af3$1@news.povray.org>
Le 13/03/2010 22:05, Kene nous fit lire :
> I am working on a math problem with POV-Ray where I am trying to calculate the
> slope of a line. I have located two points on the line with the coordinates
> (9000, -8022) and (-5000, -1792). Let us assume the first set of coordinates are
> identified as Ref1_Pos.x, Ref1_Pos.z while the second set are Ref2_Pos.x and
> Ref2_Pos.z. The slope formula will be:
> 
> Slope = ( Ref1_Pos.z - Ref2_Pos.z )/( Ref1_Pos.x - Ref2_Pos.x )
> 
> In manual calculation the above problem gives the result: -0.304862
> With POV-Ray I get: -0

Maybe we can see actual code ?

#declare Ref1_Pos=<9000,-8022>;

For instance, you get a .x and a .y (and .z=0)


Post a reply to this message

From: Kene
Subject: Re: Calculating slope of a line
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

From: Kene
Subject: Re: Calculating slope of a line
Date: 14 Mar 2010 07:35:01
Message: <web.4b9cc8b97ca26fad772dd76f0@news.povray.org>
Answer from POV-Ray to the following math:

#local SL = 8 / 12;

is the number 1.

Is there another way to formulate this so POV-Ray can provide the right answer?


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Calculating slope of a line
Date: 14 Mar 2010 07:45:48
Message: <4b9ccc6c$1@news.povray.org>
On 14.03.10 12:30, Kene wrote:
> Answer from POV-Ray to the following math:
>
> #local SL = 8 / 12;
>
> is the number 1.
>
> Is there another way to formulate this so POV-Ray can provide the right answer?

Yes, knowing how to use the str function when printing numbers by reading 
the documentation: <http://www.povray.org/documentation/view/3.6.1/232/>


Post a reply to this message

From: Kene
Subject: Re: Calculating slope of a line
Date: 14 Mar 2010 07:50:00
Message: <web.4b9ccc937ca26fad772dd76f0@news.povray.org>
"Kene" <nomail@nomail> wrote:
> Answer from POV-Ray to the following math:
>
> #local SL = 8 / 12;
>
> is the number 1.
>
> Is there another way to formulate this so POV-Ray can provide the right answer?

Pardon me, I have realized that this wrong answer is because of my settings not
POV-Ray. >Sigh!<


Post a reply to this message

From: Le Forgeron
Subject: Re: Calculating slope of a line
Date: 14 Mar 2010 08:19:30
Message: <4b9cd452$1@news.povray.org>
Le 14/03/2010 12:30, Kene nous fit lire :
> Answer from POV-Ray to the following math:
> 
> #local SL = 8 / 12;
> 
> is the number 1.
> 
> Is there another way to formulate this so POV-Ray can provide the right answer?
> 
> 

These are not the droids you are looking for:

#local SL = 8 / 12;
#debug concat(str(SL,0,0),"\n")
#debug concat(str(SL,8,8),"\n")


Post a reply to this message

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