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

Username:   Password: 
 RegisterRegister   
 Customized Circle Drawing
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cezna




PostPosted: Tue Jun 08, 2010 11:12 am   Post subject: Customized Circle Drawing

This is a program I made to draw a circle, without using the drawfilloval procedure.
I didn't make it into it's own procedure, but it would be very easy to do (if anyone wants to, feel free).
This is mostly just an exploration into how to use the math behind circles in Turing.

All of the variables are up at the top and easy to change, and I think I commented it enough to allow it to be understandable.

If anyone likes it, please post back and let me know. Very Happy

Turing:

var posx, posy, y1, y2, radius, drawx, clr : real
var thickness : int
% two y values (y1 and y2) are necessary because at each x coordinate, there is a top and bottom y coordinate
radius := 50
posx := 100 % x position of centre of cirlce
posy := 100 % y position of centre of cirlce
clr := 31 % colour of cirlce
thickness := 2 % thickness of cirlce in pixels

setscreen ("graphics:200;200, nobuttonbar, title:Circle Draw")
colourback (black)
cls

% calculates y values for each x coordinate
function Y (x, radius : real) : real
    if radius ** 2 - x ** 2 > 0 then
        result sqrt (radius ** 2 - x ** 2)
    else
        result - sqrt (- (radius ** 2 - x ** 2)) % avoids 'negative value passed to Sqrt' error
    end if
end Y

for count : 0 .. thickness - 1
    clr_draw := clr
    drawx := count % shifts next layer over a pixel to create a thick outline
    loop
        drawx += .001
        if clr_draw > 0 and clr_draw < 255 then
            clr_draw -= .0001
        end if
        y1 := Y (drawx - radius, radius - count) + posy % calculates UPPER y coordinate
        y2 := -Y (drawx - radius, radius - count) + posy % calculates LOWER y coordinate
        drawdot (round (drawx + posx - radius), round (y1), round (clr_draw)) % draws UPPER y coordinate
        drawdot (round (drawx + posx - radius), round (y2), round (clr_draw)) % draws LOWER y coordinate
        exit when drawx >= 2 * radius - count
    end loop
end for



There's also a version that fades the colour as it goes from left to right, which I have posted as an attachment as I figure not as many people will care about it once they have seen he first one, so I am trying to keep my post size small.




Circle Draw.t
 Description:
The regular version, if anyone wants a download instead of copy paste.

Download
 Filename:  Circle Draw.t
 Filesize:  1.37 KB
 Downloaded:  143 Time(s)


Circle Draw (clr fade).t
 Description:
The fading colour version.

Download
 Filename:  Circle Draw (clr fade).t
 Filesize:  1.4 KB
 Downloaded:  107 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
copthesaint




PostPosted: Tue Jun 08, 2010 11:27 am   Post subject: Re: Customized Circle Drawing

Not trying to discourage you or anything, but this really isnt worth posting, since the same thing can be done with

Turing:
for i : 0..359
    drawdot (round (10* cos (i)) +50,round (10* sin (i))+50, 24 + round (7* sin (i)))
end for


which is 1000* faster, and even this should not be used...
if you have to draw a circle just stick to the predef Wink
Cezna




PostPosted: Tue Jun 08, 2010 11:40 am   Post subject: RE:Customized Circle Drawing

The first part does make me feel like a fail Sad
But as for the predef, this was just an exploration into the math behind circle drawing.... and at that it seems to have been a failure.
chrisbrown




PostPosted: Tue Jun 08, 2010 12:54 pm   Post subject: RE:Customized Circle Drawing

Failures are just learning opportunities. You won't find better methodologies until you first explore the ones that make sense to you.
Cezna




PostPosted: Tue Jun 08, 2010 5:09 pm   Post subject: RE:Customized Circle Drawing

Thank you chrisbrown, I suppose you're right.
At least mine draws it slowly if the x changes slow enough, that's kinda cool...
Well, maybe not so much
copthesaint




PostPosted: Tue Jun 08, 2010 7:58 pm   Post subject: RE:Customized Circle Drawing

Discovery channel Smile Civilization was not built on success Smile
Cezna




PostPosted: Wed Jun 09, 2010 7:58 pm   Post subject: RE:Customized Circle Drawing

Alright, I am feeling a little better about my attempt now.

Thanks again for the simplified version copthesaint.
DtY




PostPosted: Wed Jun 09, 2010 8:49 pm   Post subject: Re: Customized Circle Drawing

copthesaint @ Tue Jun 08, 2010 11:27 am wrote:
Not trying to discourage you or anything, but this really isnt worth posting, since the same thing can be done with

Turing:
for i : 0..359
    drawdot (round (10* cos (i)) +50,round (10* sin (i))+50, 24 + round (7* sin (i)))
end for


which is 1000* faster, and even this should not be used...
if you have to draw a circle just stick to the predef Wink
The problem with this is that it will only draw 360 dots, drawing 360 dots around a point in a circle does not make a circle, unless the circumference of the circle is less than 360px (with the possibility that some points will end up on the same spot, due to round off problems), which means that you can only use this to draw a circle that is less than ~58px in radius.

A better solution would be to draw lines between each of the dots, which would at least always draw a closed shape, but would look like a polygon if it was big. There's probably a formula to figure out how many sides need to be drawn to create a decent looking circle.

I actually wrote a program a while ago that would draw a circle (in C, into a 2D array, saved as an XPM) by iterating over every pixel on the canvas, and changing the colour if the distance between it and the centre was less than the circle radius.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: