
-----------------------------------
Cezna
Tue Jun 08, 2010 11:12 am

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.  :D 


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.



-----------------------------------
copthesaint
Tue Jun 08, 2010 11:27 am

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 

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 ;)

-----------------------------------
Cezna
Tue Jun 08, 2010 11:40 am

RE:Customized Circle Drawing
-----------------------------------
The first part does make me feel like a fail :(
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
Tue Jun 08, 2010 12:54 pm

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
Tue Jun 08, 2010 5:09 pm

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
Tue Jun 08, 2010 7:58 pm

RE:Customized Circle Drawing
-----------------------------------
Discovery channel :) Civilization was not built on success :)

-----------------------------------
Cezna
Wed Jun 09, 2010 7:58 pm

RE:Customized Circle Drawing
-----------------------------------
Alright, I am feeling a little better about my attempt now.

Thanks again for the simplified version copthesaint.

-----------------------------------
DtY
Wed Jun 09, 2010 8:49 pm

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 

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 ;)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.
