Dark Bit Factory & Gravity

ARCHIVE => Archive => Cobra => Topic started by: mike_g on March 07, 2007

Title: My First Cobra Prog
Post by: mike_g on March 07, 2007
I finally got around to trying out Cobra. So far I quite like it :)

The prog is a conversion of the Circle sector drawing function that I wrote in Blitz. I tried to make it better by adding a fill parameter, using the cobra FloodFill() command. I'm doing something wrong though cos sometimes its filling outside the sector.
Code: [Select]
program
    uses pure2d,keyset

function DRAW_SECTOR(cx, cy, inner_rad, outer_rad, start_deg, sector_deg, fill, r, g, b): boolean
var
    increment: real
    steps: integer
    degrees: real
    x, y, i: integer
    x1, y1, x2, y2: integer
    colour: integer

begin
    colour = ToRGBA(r, g, b)
    //DRAW OUTER CURVE
    increment = toreal(sector_deg / (outer_rad * Pi *2))
    steps = sector_deg / increment
    degrees = start_deg
    for i = 0 to steps+1
        x = cx - outer_rad * Cos(degrees)
        y = cy - outer_rad * Sin(degrees)
        Pixel(x, y, colour)
        degrees = degrees + increment
    next   

    //DRAW INNER CURVE
    increment = toreal(sector_deg /(inner_rad * Pi * 2))
    steps = sector_deg / increment
    degrees = start_deg
    for i = 0 to steps+1
        x = cx - inner_rad * Cos(degrees)
        y = cy - inner_rad * Sin(degrees)
        Pixel(x, y, colour)
        degrees = degrees + increment   
    next   
 
    //DRAW LINES
    x1 = (cx - inner_rad * Cos(start_deg))
    y1 = (cy - inner_rad * sin(start_deg))
    x2 = (cx - outer_rad * Cos(start_deg))
    y2 = (cy - outer_rad * sin(start_deg))
    Line (x1, y1, x2, y2, colour)
    x1 = (cx - inner_rad * Cos(start_deg + sector_deg))
    y1 = (cy - inner_rad * sin(start_deg + sector_deg))
    x2 = (cx - outer_rad * Cos(start_deg + sector_deg))
    y2 = (cy - outer_rad * sin(start_deg + sector_deg))
    Line (x1, y1, x2, y2, colour) 
   
    if fill = 1
        x = (cx - ((inner_rad + outer_rad) / 2 )* Cos((start_deg + sector_deg) / 2))
        y = (cy - ((inner_rad + outer_rad) / 2 )* sin((start_deg + sector_deg) / 2))
        FloodFill(x, y, colour, colour, TRUE)
    endif
end

//EXAMPLE PROGRAM   
var
    i:integer
    a, b: integer

begin
    openscreen
    a=2
    b=1
   
    while not KeyDown(vk_escape)
        cls
        DRAW_SECTOR(200, 200, 60, 100, i, 270, 1, 150, 0, 0) 
        DRAW_SECTOR(200, 200, 20, 50, -i, 270, 1, 100, 0, 0)
       
        DRAW_SECTOR(500, 200, 1+a, 60+a, i+240, 60, 0, 255, 255, 255)
        DRAW_SECTOR(500, 200, 1+a, 60+a, i+120, 60, 0, 255, 255, 255)
        DRAW_SECTOR(500, 200, 1+a, 60+a, i, 60, 0, 255, 255, 255)         
       
        a=a+b
        if a > 100 then b = -b
        if a < 2 then b =-b
                 
        i=i+1
        if i = 360 then i = 0
        flip
    wend
end
Title: Re: My First Cobra Prog
Post by: Shockwave on March 07, 2007
Wow! It crashed my ide! I don't know why.
Have some good Karma for that :D
Title: Re: My First Cobra Prog
Post by: mike_g on March 07, 2007
Well I guess I must be dangerous then :D 

Not that I can help it, as it hasent crashed my IDE yet  ???
Title: Re: My First Cobra Prog
Post by: Shockwave on March 07, 2007
Graham will know why it dies :)
Title: Re: My First Cobra Prog
Post by: GrahamK on March 08, 2007
No crash here, hmm, odd.
You on the latest patch shocky ? (issued sunday).
Title: Re: My First Cobra Prog
Post by: Shockwave on March 08, 2007
Not yet Graham. I've been overbusy and haven't had chance to update...
Title: Re: My First Cobra Prog
Post by: GrahamK on March 08, 2007
huh, any excuse ;)

Anyways, here we go...
My comments flagged with - GAK.

Code: [Select]
program
    uses pure2d,keyset

function DRAW_SECTOR(cx, cy, inner_rad, outer_rad, start_deg, sector_deg, fill, r, g, b): boolean
var
    increment: real
    steps: integer
    degrees: real
    x, y, i: integer
    x1, y1, x2, y2: integer
    colour: integer
   
    lx1,ly1,lx2,ly2:real
begin
    colour = ToRGBA(r, g, b)
    //DRAW OUTER CURVE
    increment = toreal(sector_deg / (outer_rad * Pi *2))
    steps = sector_deg / increment
    degrees = start_deg
    for i = 0 to steps+1
        x = cx - outer_rad * Cos(degrees)
        y = cy - outer_rad * Sin(degrees)
        Pixel(x, y, colour)
        degrees = degrees + increment
    next
   
    // using floating point, there will be a level of rounding error, so to make sure we draw to the same place as last
    // Pixel (ie, no gaps) we record the final x,y value plotted to use when drawing last Line between the two parts - GAK
    lx1 = x
    ly1 = y   

    //DRAW INNER CURVE
    increment = toreal(sector_deg /(inner_rad * Pi * 2))
    steps = sector_deg / increment
    degrees = start_deg
    for i = 0 to steps+1
        x = cx - inner_rad * Cos(degrees)
        y = cy - inner_rad * Sin(degrees)
        Pixel(x, y, colour)
        degrees = degrees + increment   
    next
       
    // using floating point, there will be a level of rounding error, so to make sure we draw to the same place as last
    // Pixel (ie, no gaps) we record the final x,y value plotted to use when drawing last Line between the two parts - GAK
    lx2 = x
    ly2 = y   
 
    //DRAW LINES
    x1 = (cx - inner_rad * Cos(start_deg))
    y1 = (cy - inner_rad * sin(start_deg))
    x2 = (cx - outer_rad * Cos(start_deg))
    y2 = (cy - outer_rad * sin(start_deg))
    Line (x1, y1, x2, y2, colour)
   
(* don't need these anymore - GAK     
    x1 = (cx - inner_rad * Cos(start_deg + sector_deg))
    y1 = (cy - inner_rad * sin(start_deg + sector_deg))
    x2 = (cx - outer_rad * Cos(start_deg + sector_deg))
    y2 = (cy - outer_rad * sin(start_deg + sector_deg))
*)
// last Line is drawn between the final pixels plotted on inner and outer arc - GAK   
    Line (lx1, ly1, lx2, ly2, colour) 
   
    if fill = 1

(* Original calculation was the problem, averaging start, and size, not start and end of sector - GAK
        x = (cx - ((inner_rad + outer_rad) / 2 )* Cos((start_deg + sector_deg) / 2))
        y = (cy - ((inner_rad + outer_rad) / 2 )* sin((start_deg + sector_deg) / 2))
*)

// instead, add half the sector size, and that brings it into the middle - GAK
        x = (cx - ((inner_rad + outer_rad) / 2 )* Cos(start_deg + (sector_deg / 2)))
        y = (cy - ((inner_rad + outer_rad) / 2 )* sin(start_deg + (sector_deg / 2)))
        FloodFill(x, y, colour, colour, TRUE)
    endif
end

//EXAMPLE PROGRAM   
var
    i:integer
    a, b: integer

begin
    openscreen
    a=2
    b=1
   
    while not KeyDown(vk_escape)
        cls
       
        DRAW_SECTOR(200, 200, 60, 100, i, 270, 1, 150, 0, 0) 
        DRAW_SECTOR(200, 200, 20, 50, -i, 270, 1, 100, 0, 0)
       
        DRAW_SECTOR(500, 200, 1+a, 60+a, i+240, 60, 0, 255, 255, 255)
        DRAW_SECTOR(500, 200, 1+a, 60+a, i+120, 60, 0, 255, 255, 255)
        DRAW_SECTOR(500, 200, 1+a, 60+a, i, 60, 0, 255, 255, 255)         
       
        a=a+b
        if a > 100 then b = -b
        if a < 2 then b =-b
                 
        i=i+1
        if i = 360 then i = 0
        flip
    wend
end

Title: Re: My First Cobra Prog
Post by: mike_g on March 08, 2007
Thanks for the help Graham :)  I'll check out the prog, and comments when I get home.