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

Username:   Password: 
 RegisterRegister   
 N Sided Figures
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mirhagk




PostPosted: Mon Oct 04, 2010 10:43 pm   Post subject: N Sided Figures

I was working on a few ideas for a game roughly based on Flatland (the book). It is a two dimensional world, and I would set the game as playing as a sort of god-like figure that would control the little shapes.

In order to do this I would need to draw shapes of any type. I came up with a few ideas, the worst of which was switching between different functions for each shape, or even using pictures.

Then I remembered, in Flatland the highest level shape was the one closest to a circle, so I started with a circle. If I took two points on that circle and connected them, I'd get a line. If I took 3, I'd get a triangle. 4 would be a square, and n would be a n-sided shape. The method accepts 5 arguments. The first two are the x and y of the CENTER of the shape. The third is the number of sides it has. The fourth is the radius of the circle it draws points from, which controls the size of the figure. The last argument is used to rotate the figure by any angle (rotations are around the center in a counter-clockwise direction)

So what you may ask? Well haven't you ever wanted to maybe draw a triangle, or a pentagon, but didn't want to have to define every point. Or maybe you wanted to draw a square, but with the ability for it to rotate. Well now you can, and here is my code. If anyone has some suggestions for performance boosts, I'm open to them (I'm thinking of a system right now that will not need an array, only the first point and the current point need to be stored)

Turing:

proc DrawNSidedShape (x, y, n, r, rot : int) %x and y position, number of sides, radius, rotation
    var disx, disy : real %the distance from the center to the current point
    var angle : real %the angle around the circle
    var pointsx : array 1 .. n of int %arrays to store the points
    var pointsy : array 1 .. n of int
    for i : 1 .. n %%for every side
        angle := 360 / n * (i - 1) + 90 + rot %the angle is 360/number of sides*what side it is
        %I subtract one so it starts at 0, add the rotation and 90
        %which just centers it at the top
        disx := cosd (angle) * r %calculate the distance in x cosine of angle * radius
        disy := sind (angle) * r %same as above except with y and sine
        pointsx (i) := x + round (disx) %add this to the list of points
        pointsy (i) := y + round (disy)
    end for
    for i : 1 .. n - 1 %for every point except the last
        %draw a point connecting it to the nect one
        drawline (pointsx (i), pointsy (i), pointsx (i + 1), pointsy (i + 1), black)
    end for
    %connect the last one to the first one
    drawline (pointsx (1), pointsy (1), pointsx (n), pointsy (n), black)
    %colours may be changed, this last line simply fills it with green
    drawfill (x, y, brightgreen, black)
end DrawNSidedShape
var win : int := Window.Open ("position:left;top")
loop
    var numSides : int
    get numSides
    exit when numSides = 0
    DrawNSidedShape (maxx div 2, maxy div 2, numSides, maxy div 2, 0)
    Input.Pause
    drawoval (maxx div 2, maxy div 2, maxy div 2, maxy div 2, black)
    Input.Pause
    cls
end loop
Window.Close (win)
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Oct 05, 2010 4:20 am   Post subject: RE:N Sided Figures

Not a bad implementation (specifically, the method signature is well-done), but you should be able to figure out a way to do this without using arrays. Doing so will improve the performance of your drawing algorithm, letting you draw more complex scenes.

You probably also want a parameter for draw-colour.
mirhagk




PostPosted: Tue Oct 05, 2010 7:46 am   Post subject: RE:N Sided Figures

Or would it be faster to leave the array and call draw polygon, it would severly decrease the amount of drawing calls.
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: