Isometrical depiction of 3d plots

Share your advanced PureBasic knowledge/code with the community.
DerMeister
New User
New User
Posts: 3
Joined: Thu Jun 23, 2011 5:22 pm

Isometrical depiction of 3d plots

Post by DerMeister »

Some time ago i created this, inspired by my math lessons :) You can add plots in the code by using the procedure Plot3D() and view them displayed in a coordinate system. The screen width and height can also be adapted to the resolution of your screen. Maybe i sometimes didn't use the correct English mathematical terms...
Feedback from anyone is appreciated ;)

Code: Select all

;Author: DerMeister
;Date: 6/23/2011

Procedure Check(value.l, text$)
  If value=0
    MessageRequester("Error", text$)
    End
  EndIf
EndProcedure

Procedure.f Sine(Number.f)
  Ergebnis.f = Sin(Number.f * #PI / 180 )
  ProcedureReturn Ergebnis
EndProcedure

Procedure.f Cosine(Number.f)
  Ergebnis.f = Cos(Number.f * #PI / 180)
  ProcedureReturn Ergebnis
EndProcedure

Procedure.f Tangent(Number.f)
  Ergebnis.f = Tan(Number.f * #PI / 180)
  ProcedureReturn Ergebnis
EndProcedure

Procedure.f Distance(x1.w,y1.w,x2.w,y2.w)
  ProcedureReturn Sqr(Pow((x1-x2),2) + Pow((y1-y2),2))
EndProcedure

;-Variables/Constants
#Width = 1280
#Height = 1024
#Depth = 32

#Base = 15


#OX = #Width/2
#OY = #Height/2

Global YDistance = (#Height-Tangent(30)*#Width)/2

X12Count = Distance(0,#Height-YDistance,#OX,#OY)/#Base
X3Count = #Height/2/#Base

Global Sin30.f = Sine(30)
Global Cos30.f = Cosine(30)


Procedure.w XPos(x1.f,x2.f,x3.f=0)
  x = #OX
  ;X1
  x - Cos30*x1*#Base
  ;X2
  x + Cos30*x2*#Base
  ProcedureReturn x
EndProcedure

Procedure.w YPos(x1.f,x2.f,x3.f)
  y = #OY
  ;X1
  y + Sin30*x1*#Base
  ;X2
  y + Sin30*x2*#Base
  ;X3
  y - x3*#Base
  ProcedureReturn y
EndProcedure

Procedure Plot3D(x1.f,x2.f,x3.f)
  x = XPos(x1,x2,x3)
  y = YPos(x1,x2,x3)
  
  ;Draw Plot
  Circle(x,y,#Base/4,RGB(255,0,0))
  
  ;Draw Parallels
  LineColor = 0

  If x2 <> 0 And (x1 = 0 XOr x3 = 0)
    LineXY(x,y,XPos(0,x2,0),YPos(0,x2,0),LineColor)
  EndIf

  If x3 <> 0 And (x1 = 0 XOr x2 = 0)
    LineXY(x,y,XPos(0,0,x3),YPos(0,0,x3),LineColor)
  EndIf

  If x1<> 0 And (x2 = 0 XOr x3 = 0)
    LineXY(x,y,XPos(x1,0,0),YPos(x1,0,0),LineColor)
  EndIf
  
  If x1 <> 0 And x2 <> 0 And x3 <> 0
    LineXY(x,y,XPos(0,x2,x3),YPos(0,x2,x3),LineColor)
    LineXY(XPos(x1,x2,0),YPos(x1,x2,0),XPos(0,x2,0),YPos(0,x2,0),LineColor)
    LineXY(XPos(0,0,x3),YPos(0,0,x3),XPos(x1,0,x3),YPos(x1,0,x3),LineColor)
    
    LineXY(x,y,XPos(x1,0,x3),YPos(x1,0,x3),LineColor)
    LineXY(XPos(x1,x2,0),YPos(x1,x2,0),XPos(x1,0,0),YPos(x1,0,0),LineColor)
    LineXY(XPos(0,0,x3),YPos(0,0,x3),XPos(0,x2,x3),YPos(0,x2,x3),LineColor)
    
    LineXY(x,y,XPos(x1,x2,0),YPos(x1,x2,0),LineColor)
    LineXY(XPos(x1,0,x3),YPos(x1,0,x3),XPos(x1,0,0),YPos(x1,0,0),LineColor)
    LineXY(XPos(0,x2,x3),YPos(0,x2,x3),XPos(0,x2,0),YPos(0,x2,0),LineColor) 
  EndIf
EndProcedure

Check(InitSprite(),"Can't initialize DirectX")
Check(InitKeyboard(),"Can't initialize Keyboard")

Check(OpenScreen(#Width,#Height,#Depth,"Isometry"),"Can't open Screen")

Repeat
  ExamineKeyboard()
  ClearScreen(RGB(255,255,255))
  
  StartDrawing(ScreenOutput())
  
  ;X1-Axis
  LineXY(-2,#Height-YDistance,#Width-2,YDistance,0)
  LineXY(-1,#Height-YDistance,#Width-1,YDistance,0)
  LineXY(0,#Height-YDistance,#Width,YDistance,0)
  LineXY(1,#Height-YDistance,#Width+1,YDistance,0)
  LineXY(2,#Height-YDistance,#Width+2,YDistance,0)
  
  ;X2-Axis
  LineXY(-2,YDistance,#Width-2,#Height-YDistance,0)
  LineXY(-1,YDistance,#Width-1,#Height-YDistance,0)
  LineXY(0,YDistance,#Width,#Height-YDistance,0)
  LineXY(1,YDistance,#Width+1,#Height-YDistance,0)
  LineXY(2,YDistance,#Width+2,#Height-YDistance,0)
  
  ;X3-Axis
  LineXY(#OX-1,0,#OX-1,#Height,0)
  LineXY(#OX,0,#OX,#Height,0)
  LineXY(#OX+1,0,#OX+1,#Height,0)
  
  
  ;Distance/Markings
  ;X1
  For k = -X12Count To X12Count
    LineXY(XPos(k,-0.5,0),YPos(k,-0.5,0),XPos(k,0.5,0),YPos(k,0.5,0),0)
    ;Numbers
    If k%5 = 0 And k <> 0
      DrawText(XPos(k,0.5,0),YPos(k,0.5,0),Str(k),0,RGB(255,255,255))
    EndIf
  Next
  
  ;X2
  For k = -X12Count To X12Count
    LineXY(XPos(0.5,k,0),YPos(0.5,k,0),XPos(-0.5,k,0),YPos(-0.5,k,0),0)
    ;Numbers
    If k%5 = 0 And k <> 0
      DrawText(XPos(0.5,k,0)-TextWidth(Str(k)),YPos(0.5,k,0),Str(k),0,RGB(255,255,255))
    EndIf
  Next
  
  ;X3
  For k = -X3Count To X3Count
    If k <> 0
      LineXY(#OX-#Base/2,#OY+#Base*k,#OX+#Base/2,#OY+#Base*k,0)
    EndIf
    ;Numbers
    If k%5 = 0 And k <> 0
      DrawText(XPos(0,0,k)+#Base/2+TextWidth(Str(k))/2,YPos(0,0,k)-TextHeight(Str(k))/2,Str(k),0,RGB(255,255,255))
    EndIf
  Next
  
  
  ;-Draw Plots
  
  Plot3D(-15,10,5)
  Plot3D(20,10,0)
  Plot3D(0,-10,10)
  
  
  StopDrawing()
  
  FlipBuffers()
  Delay(1)
Until KeyboardPushed(#PB_Key_Escape)
Last edited by DerMeister on Sat Jun 25, 2011 9:33 am, edited 1 time in total.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Isometrical depiction of 3d plots

Post by Comtois »

Hello,

Nice code :)
Btw you can use Radian()

Code: Select all

Global Sin30.f = Sin(Radian(30))
Please correct my english
http://purebasic.developpez.com/
DerMeister
New User
New User
Posts: 3
Joined: Thu Jun 23, 2011 5:22 pm

Re: Isometrical depiction of 3d plots

Post by DerMeister »

Comtois wrote:Nice code :)
Thanks, I'm glad you like it :)
Comtois wrote:Btw you can use Radian()
Interesting, I overlooked this function until now. Anyone else want to comment on the code or give an improvement?
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Isometrical depiction of 3d plots

Post by kenmo »

Looks good, nice and clean display. Thanks for sharing this.

( PS. I had to change the resolution.

The first time I ran it, I got the messagerequester saying the 1280x1024 screen couldn't be opened, but then the program locked up, apparently trying to run screen commands.

When you check the OpenScreen command, you should avoid running the rest of the program if it failed! )
DerMeister
New User
New User
Posts: 3
Joined: Thu Jun 23, 2011 5:22 pm

Re: Isometrical depiction of 3d plots

Post by DerMeister »

kenmo wrote:Looks good, nice and clean display. Thanks for sharing this.
Thanks for the comment :)
kenmo wrote:When you check the OpenScreen command, you should avoid running the rest of the program if it failed! )
Yes, I just noticed there is the "End" command missing in the Check() Function. It actually should end the program if DirectX or Keyboard couldn't be initialized or the Screen couldn't be opened...
I will edit it in the code. Also thanks for correcting....
Post Reply