# [fpc-pascal] Calculating Pixels to represent 3D coordinates

Thomas Young tygraphics at icloud.com
Thu Sep 19 20:54:58 CEST 2019

```James, are you not applying the rotation universally to all coordinates? If so, you are better off pre-calculating your directional sines and cosines.
For example:
TH: REAL;   {ROTATIONS (in degrees) AROUND Z-AXIS BY theta, LEFTRIGHT-> turn its back to us}

PHI: REAL; {ROTATIONS (in degrees) AROUND X-AXIS BY phi, UPDOWN-> a nod}

PSI: REAL; {ROTATIONS (in degrees) AROUND Y-AXIS BY psi, SPIN-> a clock face}
P
STH: REAL;{SIN(TH * (PI / 180)), CONVERT DEGREES TO RADIANS}
CTH: REAL;{COS(TH * (PI / 180)), CONVERT DEGREES TO RADIANS}
STI: REAL;{SIN(PHI * (PI / 180)), CONVERT DEGREES TO RADIANS}
CTI: REAL;{COS(PHI * (PI / 180)), CONVERT DEGREES TO RADIANS}
STS: REAL;{SIN(PSI * (PI / 180)), CONVERT DEGREES TO RADIANS}
CTS: REAL;{COS(PSI * (PI / 180)), CONVERT DEGREES TO RADIANS}

Thomas Young
330-256-7064
Sent from my iPhone

> On Sep 19, 2019, at 12:38 PM, James Richters <james at productionautomation.net> wrote:
>
> Thank you for the help and suggestions with this.  After playing with the suggested formulas I was able to figure out how they work...  The solution to this is probably somewhere but I ended up just doing it myself.
>
> I realized that Thomas' formula
> var H1 = (Y - X) * 0.86602 + ScreenOrgin_H; var V1 = (X + Y) * 0.5 - Z + ScreenOrgin_V;
> was multipying by the cosine of 30 degrees for H1 and the Sine of 30 degrees for V1....
>
> and Gustavo's forumla was basically the same thing but multiplying by the cosine and sine of 45 degrees...
>
> so I realized that all I really want to do has nothing to do with camera angles, or projections or anythng else... I just strictly need do the 3D rotations then just display the resulting X, and Y coordinate and ignore Z.       My original 2D representaion showing only X and Y is as if I am looking perfectly straight down at it.. so I can't see the Z only lines, but after I perform the 3D rotations now I can see the Z component as they end up being represented on the X and Y axis.... but I still act like I am still looking straight down at it, so I only plot X and Y.
> I checked the results of my formula against my CAD program everything looks EXACTLY the same no matter how I change the rotations,  so this must be what the CAD program is doing as well.
>
> Here are the formula I came up with that allows me to adjust the 3 possible rotations,  A - rotating around the X axis, B rotating around the Y axis, and C rotating around the Z axis.
>
> X1 - initial X coordinate
> Y1 - initial Y coordinate
> Z1 - initial Z coordinate
> X2 - output  X coordinate
> Y2 - output Y coordinate
>
> //XY Rotation - only rotates X and Y around the Z axis (C Rotation)
>  XC_Point := ((CoSine(C_Angle) * (X1-XC_Center)) - (Sine(C_Angle) * (Y1-YC_Center) )) + XC_Center;
>  YC_Point := ((CoSine(C_Angle) * (Y1-YC_Center)) + (Sine(C_Angle) * (X1-XC_Center) )) + YC_Center;
>  ZC_Point :=Z1;
>
> //XZ Rotation - only rotates X and Z around the Y axis (B Rotation)
>  XB_Point := ((CoSine(B_Angle) * (XC_Point-XB_Center)) + (Sine(B_Angle) * (ZC_Point-ZB_Center) )) + XB_Center;
>  YB_Point := YC_Point;
>  ZB_Point :=  ((CoSine(B_Angle) * (ZC_Point-ZB_Center)) - (Sine(B_Angle) * (XC_Point-XB_Center) )) + ZB_Center;
>
> //YZ Rotation  - only rotates Y and Z around the X axis (A Rotation)
>  XA_Point := XB_Point;
>  YA_Point := ((CoSine(A_Angle) * (YB_Point-YA_Center)) - (Sine(A_Angle) * (ZB_Point-ZA_Center) )) + at YA_Center;
>  ZA_Point := ((CoSine(A_Angle) * (XB_Point-XA_Center)) + (Sine(A_Angle) * (ZB_Point-ZA_Center) )) + XA_Center;     // useless to plot point, just for information
>
> X2 := Scale * XA_Point+X_Offset;
> Y2 := Scale * YA_Point+Y_Offset;
>
> Notes:
>
> My fuctions CoSine and Sine convert the angle to radians from given degrees.
>
> I have my 0,0 located in the lower left hand corner, but PTC-Graph has it in the upper left corner.. I used the above formula before the section that takes care of getting it on the screen, I don't know if some adjustment would be nessecary for use on direct screen coordinates... things might end up upside down, or the rotations might need to be reversed by switching the + and - between CoSine and Sine because of this..
>
>
> Thank you again for all the help and suggestions.  I also found the websites and books on the subject very interesting as well.
>
> James
>
>
> -----Original Message-----
> From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Thomas Young via fpc-pascal
> Sent: Tuesday, September 17, 2019 5:00 PM
> To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
> Cc: Thomas Young <tygraphics at icloud.com>
> Subject: Re: [fpc-pascal] Calculating Pixels to represent 3D coordinates
>
> This is an isometric projection I use:
> var H1 = (Y - X) * 0.86602 + ScreenOrgin_H; var V1 = (X + Y) * 0.5 - Z + ScreenOrgin_V;
>
> Thomas Young
> 330-256-7064
> Sent from my iPhone
>
>> On Sep 17, 2019, at 4:53 PM, Gustavo Enrique Jimenez <gejimenez at gmail.com> wrote:
>>
>> A simple transformation is:
>>
>> P3D=(X,Y,Z)
>> P2D=(x,y)
>>
>> x=X+Y*0.707
>> y=Y*0.707+Z
>>
>> I did not tried it, but I think that this is the transformation that
>> you are looking for.
>>
>>
>> Gustavo
>>
>> El mar., 17 sept. 2019 a las 17:37, James Richters
>> (<james at productionautomation.net>) escribió:
>>>
>>>> What exactly are you trying to do? Usually if you’re doing 3D this all happens on the GPU and you get back a color/depth buffer. Maybe you need to know where a 2D coordinate is in 3D space?
>>>
>>> What I'm trying to do is much simpler than rendering a 3D object..  All I'm trying to do is display a 3D line drawing or wireframe on the screen.  I don't need it to dynamically rotate or anything, and it doesn't need to show any surfaces, textures, lighting, reflections, or shadows, just give a representation of the XYZ points and lines connecting 2 pair of XYZ coordinates on the screen.   The purpose of this is to show a 3D representation of a CNC tool path including the Z movements.
>>>
>>> James
>>> _______________________________________________
>>> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>> _______________________________________________
>> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

```