Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math, Vectors and Drawing 3D stuff
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Mon Mar 29, 2010 3:34 pm   Post subject: Math, Vectors and Drawing 3D stuff

I decided today to put the math I'm being taught to use for once, so I wrote this program (unfinished) that will eventually take in an equation (that part will be messy and is irrelevant to this issue) and display it on a 3D axis. Something's wrong however, and it doesn't draw the axis properly (I will be making it more modular later, so you can adjust the scale and whatnot). As far as I can tell, my math is right. Can anyone figure out what's wrong?

Turing:

type Vector : %Algebraic Vector type
    record
        x, y, z : real
    end record

function DotProduct (a, b : Vector) : real %returns the dot product of two vectors
    result a.x * b.x + a.y * b.y + a.z * b.z
end DotProduct

function CrossProduct (a, b : Vector) : Vector %Returns the dot product vector of 2 vectors
    var c : Vector
    c.x := (a.y * b.z) - (a.z * b.y)
    c.y := (a.z * b.x) - (a.x * b.z)
    c.z := (a.x * b.y) - (a.y * b.x)
    result c
end CrossProduct

function VectorLength (a:Vector):real %Returns the length of vector a
    result sqrt (a.x **2 + a.y **2 + a.z **2)
end VectorLength

function UnitVector (a:Vector) : Vector %returns the unit vector a vector
    var b : Vector
    var vector_length := VectorLength (a)
    b.x := a.x / vector_length
    b.y := a.y / vector_length
    b.z := a.z / vector_length
    result b
end UnitVector



function ScalarMultiple (a : Vector, b : real) : Vector %multiplies vector a by scalar b
    var c : Vector
    c.x := a.x * b
    c.y := a.y * b
    c.z := a.z * b
    result c
end ScalarMultiple

function Proj (a:Vector) : Vector %returns the projection of a 3d vector onto a 2d plane
    var b : Vector
    var c : Vector
    b.x := 1
    b.y := 1
    b.z := 0
    c := ScalarMultiple (b, DotProduct (a, b)/(VectorLength(b)**2))
    result c
end Proj

proc DrawDot3D (a : Vector) %draws a dot at the end of vector a
   var projection : Vector := Proj (a)
    Draw.Dot (round (projection.x), round (projection.y), black)
end DrawDot3D

proc DrawLine3D (a, b : Vector) %draws a line from vector a to vector b
    var projA := Proj (a)
    var projB := Proj (b)
    Draw.Line (round (projA.x), round (projA.y), round (projB.x), round (projB.y), black)
end DrawLine3D

proc DrawAxis
    var xAxisTop : Vector
    var xAxisBottom : Vector
    var yAxisTop : Vector
    var yAxisBottom : Vector
    var zAxisTop : Vector
    var zAxisBottom : Vector
   
    xAxisTop.x := 350
    xAxisTop.y := 0
    xAxisTop.z := 0
   
    xAxisBottom.x := 50
    xAxisBottom.y := 0
    xAxisBottom.z := 0
   
    yAxisTop.x := 0
    yAxisTop.y := 350
    yAxisTop.z := 0
   
    yAxisBottom.x := 0
    yAxisBottom.y := 50
    yAxisBottom.z := 0
   
    zAxisTop.x := 0
    zAxisTop.y := 0
    zAxisTop.z := 350
   
    zAxisBottom.x := 0
    zAxisBottom.y := 0
    zAxisBottom.z := 50
   
    DrawLine3D (xAxisTop, xAxisBottom)
    DrawLine3D (yAxisTop, yAxisBottom)
    DrawLine3D (zAxisTop, zAxisBottom)
end DrawAxis

DrawAxis


Yes, the DrawAxis function is really messy at the moment, that was just a hackjob to make sure my math worked. Which is doesn't. I think it might have something to do with my Proj function, though I can't for the life of me figure out what's wrong. Maybe it's 'cause I used the equation to project a line onto another line rather than a plane? I dunno if it makes a difference.
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Mon Mar 29, 2010 3:49 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

Are you guys already learning vectors in calculus!? We are so behind... Sad
Insectoid




PostPosted: Mon Mar 29, 2010 4:05 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

We're haven't done calculus yet, so you're not behind (just doing things out of order).
Kharybdis




PostPosted: Mon Mar 29, 2010 4:48 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

learning calculus before vectors is better than learning vectors beforehand because it ties in with functions
Insectoid




PostPosted: Mon Mar 29, 2010 5:06 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

How about we stop talking about the school system and figure out what's wrong with my code?
Zren




PostPosted: Mon Mar 29, 2010 5:47 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

Are you trying to add perspective/camera?

If you remove the modifications in the proj() (for now just have result a), it works perfectly for a simple 3D object to 2D.
Insectoid




PostPosted: Mon Mar 29, 2010 6:08 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

It doesn't draw the axis correctly when I just result a.

I conclude that it is in my formula for proj() that my error lies.
Zren




PostPosted: Mon Mar 29, 2010 6:28 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

What's it suppose to look like?
You've got something up and down the y, x axis. and the dot at (0, 0) representing the z-axis line.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Mar 29, 2010 7:00 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

Ah, I had a little brainfart. My coordinates for the axis were wrong. Lol. Anyway, the proj() function will be needed for when rotation happens, and I can't just result A (Only reason that works now is because drawing things only looks at the X and Y values, which are already accurate for the current perspective. As soon as rotation comes in, it will break down).
Insectoid




PostPosted: Tue Mar 30, 2010 11:09 am   Post subject: RE:Math, Vectors and Drawing 3D stuff

So, I figure I need to calculate the equation for the plane representing the screen (which can change depending on the screen's orientation), then project the image onto that plane. I'll fix this up as I progress in Calc & Vectors.
Insectoid




PostPosted: Wed Mar 31, 2010 1:33 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

Sorry for the multiposting, but I've had a mind-breakthrough.

So, I figure to simulate rotation, I'm going to have a 'screen-plane' which, hence the name, is a plane representing the screen. Rather than rotating the axis, I'll be rotating this plane and projecting vectors/points onto the rotation, and then drawing the projection based on X and Y coordinates (Z will be ignored, as X and Y will already correspond to their screen locations and are the only things I need). The projection 'rays' (the line from the original point to the projected point) should be normal to the plane. I figure this will increase efficiency in both coding and runspeed, as I'll be able to generate a list of points from an equation once (which I expect to be the slowest part of the program) and then project those points onto the plane. Really, I'm switching out adjusting points for adjusting the orientation of the projection plane (which should be a lot faster).

Anyway, I still have the problems of projection onto the plane, representing the plane in code and rotating that plane. I'm not a math genius, so I may need a good bit of help with that. I don't even know where to start.
SNIPERDUDE




PostPosted: Wed Mar 31, 2010 4:09 pm   Post subject: RE:Math, Vectors and Drawing 3D stuff

I'd be very interested in seeing a demo on this, I haven't taken such an approach before.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: