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

Username:   Password: 
 RegisterRegister   
 3D Sphere Creation + Rotation
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MihaiG




PostPosted: Tue Jun 11, 2013 11:22 pm   Post subject: 3D Sphere Creation + Rotation

Heya folks, its been almost 5 years since my last submission so I decided to get out and do some fun stuff.
A lot of you will be doing math and calculus once you enter university. One important thing youll learn is coordinate systems!

One such system is the Spherical Polar Coordinates
https://en.wikipedia.org/wiki/Spherical_coordinate_system

Using just two angles we are able to address any point on a R= 1 sphere.. meaning if we were to go through every possible angle--to a degree-- we could figure out the converted points on our cartesian plane!

Woah! Using a bit of logic i decided.. well let me create a sphere first.. but it didnt look well.. it needs an aspect of volume. So for each point i calculate i determine a predetermined width and create a polygon based on our resolution of the sphere.. i even made it.. so the higher the resolution the smaller the blocks looK!

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Turing:


%%submitted by MihaiG%%

var tmax := 5%define the theta subdivission
var pmax := 5 %define the phi subdivision
var vs := 360 %define how much of the circle around horizontal axis you want to draw
var hs := 180 %define how much of the circle around vertical axis you want to draw
%ie if you do 180/90 you get a quadrant
var pts : array 0 .. (tmax + 1) * (pmax + 1), 1 .. 4, 1 .. 3 of real
%defintion of the point storage, first set is the address, second is for defining the four corners, each additional point
var a : int := 1 %counter for indexing the array, we could use something like a := theta*tmax + phy
var sx := (360 / pmax) / 2
var sy := (180 / tmax) / 2


View.Set ("offscreenonly")
setscreen ("graphics:max;max,nobuttonbar")

for theta : 0 .. tmax

    for phi : 0 .. pmax
        %
        pts (a, 1, 1) := sind (hs * (theta) / tmax - sy) * cosd (vs * (phi) / pmax - sx)
        pts (a, 1, 2) := sind (hs * (theta) / tmax - sy) * sind (vs * (phi) / pmax - sx)
        pts (a, 1, 3) := cosd (hs * (theta) / tmax - sy)
        %
        pts (a, 3, 1) := sind (hs * (theta) / tmax + sy) * cosd (vs * (phi) / pmax + sx)
        pts (a, 3, 2) := sind (hs * (theta) / tmax + sy) * sind (vs * (phi) / pmax + sx)
        pts (a, 3, 3) := cosd (hs * (theta) / tmax + sy)
        %
        pts (a, 2, 1) := sind (hs * (theta) / tmax + sy) * cosd (vs * (phi) / pmax - sx)
        pts (a, 2, 2) := sind (hs * (theta) / tmax + sy) * sind (vs * (phi) / pmax - sx)
        pts (a, 2, 3) := cosd (hs * (theta) / tmax + sy)
        %
        pts (a, 4, 1) := sind (hs * (theta) / tmax - sy) * cosd (vs * (phi) / pmax + sx)
        pts (a, 4, 2) := sind (hs * (theta) / tmax - sy) * sind (vs * (phi) / pmax + sx)
        pts (a, 4, 3) := cosd (hs * (theta) / tmax - sy)
        %
        a += 1
    end for
end for
var x, y : array 1 .. 4 of int
loop
    cls
    put tmax
    put pmax
    for i : 1 .. a - 1
        for p : 1 .. 4
            pts (i, p, 1) := pts (i, p, 1) * 0.999847695 + pts (i, p, 2) * 0.0174524064
            pts (i, p, 2) := pts (i, p, 2) * 0.999847695 - ((pts (i, p, 1) - pts (i, p, 2) * 0.0174524064) / 0.999847695) * 0.0174524064
            pts (i, p, 3) := pts (i, p, 3) * 0.999847695 + pts (i, p, 2) * 0.0174524064
            pts (i, p, 2) := pts (i, p, 2) * 0.999847695 - ((pts (i, p, 3) - pts (i, p, 2) * 0.0174524064) / 0.999847695) * 0.0174524064
            x (p) := round (5 * pts (i, p, 1) * 10 / (pts (i, p, 3) * 0.00001 + 0.2) + maxx / 2)
            y (p) := round (5 * pts (i, p, 2) * 10 / (pts (i, p, 3) * 0.00001 + .2) + maxy / 2)
            Draw.Dot (x (p), y (p), 7)
        end for
        Draw.Polygon (x, y, 4, 7)

    end for
    View.Update
end loop



The rotation stuff ill leave as an exercise to the reader.. If you have any questions or improvements let me know! This was about 2-4 hours of work through about 3 different revisions!
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jun 12, 2013 10:29 am   Post subject: Re: 3D Sphere Creation + Rotation

Not bad. As a suggestion, try eliminating the magic numbers. Is 0.999847695 = cos(1 degree)? Is 0.0174524064 = sin(1 degree)? Your code doesn't tell me.

Also, you have a bug:
Posted Image, might have been reduced in size. Click Image to view fullscreen.[/img]
MihaiG




PostPosted: Wed Jun 12, 2013 12:41 pm   Post subject: Re: 3D Sphere Creation + Rotation

DemonWasp @ Wed Jun 12, 2013 11:29 am wrote:
Not bad. As a suggestion, try eliminating the magic numbers. Is 0.999847695 = cos(1 degree)? Is 0.0174524064 = sin(1 degree)? Your code doesn't tell me.

Also, you have a bug:


Yea, i had the magic numbers for cos/sin i got lazy to change them to something else..
Regarding your bug
its because im re drawing over the poles try chaning line 18 to as well as 8

Turing:

var pts : array 0 .. round(tmax/2) * (pmax + 1), 1 .. 4, 1 .. 3 of real

for theta : 1 .. tmax-1 by 2


Seems to fix that issue of over drawing. It would draw each section twice, that small fix should address a few of the issues..

Though now tmax has to be double what your theta subdivision would be.. ie try 6/3 and you get the shape you want!

Let me know if this works for you

Here is the updated code!

Turing:

%%submitted by MihaiG%%

var tmax := 50%define the theta subdivission
var pmax := 50%define the phi subdivision
var vs := 360 %define how much of the circle around horizontal axis you want to draw
var hs := 180 %define how much of the circle around vertical axis you want to draw
%ie if you do 180/90 you get a quadrant
var pts : array 0 .. round (tmax / 2) * (pmax + 1), 1 .. 4, 1 .. 3 of real
%defintion of the point storage, first set is the address, second is for defining the four corners, each additional point
var a : int := 1 %counter for indexing the array, we could use something like a := theta*tmax + phy
var sx := (360 / pmax) / 2
var sy := (360 / tmax) / 2
var rot := 0.1


View.Set ("offscreenonly")
setscreen ("graphics:max;max,nobuttonbar")

for theta : 1 .. tmax - 1 by 2

    for phi : 0 .. pmax
        %
        pts (a, 1, 1) := sind (hs * (theta) / tmax - sy) * cosd (vs * (phi) / pmax - sx)
        pts (a, 1, 2) := sind (hs * (theta) / tmax - sy) * sind (vs * (phi) / pmax - sx)
        pts (a, 1, 3) := cosd (hs * (theta) / tmax - sy)
        %
        pts (a, 3, 1) := sind (hs * (theta) / tmax + sy) * cosd (vs * (phi) / pmax + sx)
        pts (a, 3, 2) := sind (hs * (theta) / tmax + sy) * sind (vs * (phi) / pmax + sx)
        pts (a, 3, 3) := cosd (hs * (theta) / tmax + sy)
        %
        pts (a, 2, 1) := sind (hs * (theta) / tmax + sy) * cosd (vs * (phi) / pmax - sx)
        pts (a, 2, 2) := sind (hs * (theta) / tmax + sy) * sind (vs * (phi) / pmax - sx)
        pts (a, 2, 3) := cosd (hs * (theta) / tmax + sy)
        %
        pts (a, 4, 1) := sind (hs * (theta) / tmax - sy) * cosd (vs * (phi) / pmax + sx)
        pts (a, 4, 2) := sind (hs * (theta) / tmax - sy) * sind (vs * (phi) / pmax + sx)
        pts (a, 4, 3) := cosd (hs * (theta) / tmax - sy)
        %
        a += 1
    end for
end for
var x, y : array 1 .. 4 of int
loop
    cls
    put tmax/2
    put pmax
    for i : 1 .. a - 1
        for p : 1 .. 4
            pts (i, p, 1) := pts (i, p, 1) * cosd(rot) + pts (i, p, 2) * sind(rot)
            pts (i, p, 2) := pts (i, p, 2) * cosd(rot) - ((pts (i, p, 1) - pts (i, p, 2) * sind(rot)) / cosd(rot)) * sind(rot)
            pts (i, p, 3) := pts (i, p, 3) * cosd(rot) + pts (i, p, 2) * sind(rot)
            pts (i, p, 2) := pts (i, p, 2) * cosd(rot) - ((pts (i, p, 3) - pts (i, p, 2) * sind(rot)) / cosd(rot)) * sind(rot)
            x (p) := round (5 * pts (i, p, 1) * 10 / (pts (i, p, 3) * 0.00001 + 0.2) + maxx / 2)
            y (p) := round (5 * pts (i, p, 2) * 10 / (pts (i, p, 3) * 0.00001 + .2) + maxy / 2)
            Draw.Dot (x (p), y (p), 7)
        end for
        Draw.Polygon (x, y, 4, 7)

    end for
    View.Update
end loop




Ill add in keyboard rotation when i get some time.. this was more for fun than anything, i stole some ideas from my 3d engine from 5 years ago
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: