• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

draw_circle_ext

Bingdom

Googledom
So basically i'm trying to form my own improved draw_circle called draw_circle_ext, which allows the user to determine the amount of sides the circle has. The problem i'm having is that when i want to draw the circle, i'm having issues centering it. I've managed to work out how to center the left/right but i'm having issues working out how to center the up/down.

Here is what i've done
Code:
///draw_circle_ext(x,y,radius,sides);
var xx = argument0 - (argument2/argument3)/2; //Fine
var yy = argument1 + (argument2/argument3)/2; //Can't figure out
var rad = argument2;
var sides = argument3;
var dir = 0; //Right
var xxx = xx,yyy = yy;
repeat(sides) {
    xxx += lengthdir_x(rad/sides,dir);
    yyy += lengthdir_y(rad/sides,dir);
    draw_line(xx,yy,xxx,yyy);
    xx = xxx;
    yy = yyy;
    dir += 360/sides;
}
 

jo-thijs

Member
So basically, you want to use draw_set_circle_precision?

EDIT:
Wait, what is that code supposed to do?
Draw the perimeter of the circle?
Why would a line segment have the length rad/sides?
 
Last edited:

Mr Magnus

Viking King
If you don't want draw_set_circle_precision for some weird reason don't draw a circle like this. Look up and use
draw_vertex and polygonal drawing. It's a tad faster and easier to operate.
 

Bingdom

Googledom
So basically, you want to use draw_set_circle_precision?

EDIT:
Wait, what is that code supposed to do?
Draw the perimeter of the circle?
Why would a line segment have the length rad/sides?
Yes, the code is supposed to draw a perimeter of a circle ;). The reason why i did rad/sides is to make the shape a lot smaller, and without that the shape will keep getting bigger if you add more sides.
If you don't want draw_set_circle_precision for some weird reason don't draw a circle like this. Look up and use
draw_vertex and polygonal drawing. It's a tad faster and easier to operate.


Thanks for your responses. I have looked at draw_set_circle_precision, but the problem is you can only set a certain amount of sides. Someday, i might want to draw a very large circle, and 64 sides may not be enough sides to make the circle look sharp. If i use 2D primitives, wouldn't i run into the same issue again? Or is there a different method of doing this. :)
 

jo-thijs

Member
Yes, the code is supposed to draw a perimeter of a circle ;). The reason why i did rad/sides is to make the shape a lot smaller, and without that the shape will keep getting bigger if you add more sides.
But what you're currently doing still scales wrongly.
The size of a line segment should be:
sqrt(sqr(1 - dcos(360 / sides)) + sqr(dsin(360 / sides))) * radius
which simplifies to:
sqrt(2 - dcos(360 / sides) * 2) * radius

That's quite different from just 360 / sides.

Thanks for your responses. I have looked at draw_set_circle_precision, but the problem is you can only set a certain amount of sides. Someday, i might want to draw a very large circle, and 64 sides may not be enough sides to make the circle look sharp. If i use 2D primitives, wouldn't i run into the same issue again? Or is there a different method of doing this. :)
You would, but at a waaay higher number than 64.
I'd be surprised if you'd ever need more than primitives can offer you.
I'd recommend using primitives in your case, but I'll also show you how to do it your way:
Code:
///draw_circle_ext(x,y,radius,sides)

argument3 = max(floor(argument3), 3);

var px, py, nx, ny, d;
nx = argument0 + argument2;
ny = argument1;
d = 360 / argument3;
for(var t = d; t <= 360; t += d) {
    px = nx;
    py = ny;
    px = argument0 + lengthdir_x(argument2, t);
    py = argument1 + lengthdir_y(argument2, t);
    draw_line(px, py, nx, ny);
}
 

Bingdom

Googledom
But what you're currently doing still scales wrongly.
The size of a line segment should be:
sqrt(sqr(1 - dcos(360 / sides)) + sqr(dsin(360 / sides))) * radius
which simplifies to:
sqrt(2 - dcos(360 / sides) * 2) * radius

That's quite different from just 360 / sides.


You would, but at a waaay higher number than 64.
I'd be surprised if you'd ever need more than primitives can offer you.
I'd recommend using primitives in your case, but I'll also show you how to do it your way:
Code:
///draw_circle_ext(x,y,radius,sides)

argument3 = max(floor(argument3), 3);

var px, py, nx, ny, d;
nx = argument0 + argument2;
ny = argument1;
d = 360 / argument3;
for(var t = d; t <= 360; t += d) {
    px = nx;
    py = ny;
    px = argument0 + lengthdir_x(argument2, t);
    py = argument1 + lengthdir_y(argument2, t);
    draw_line(px, py, nx, ny);
}
Thanks for the code, i will try myself to convert this into a primitive as you and Mr Magnus suggested. ;)

EDIT:
I noticed a typo,
px = argument0 + lengthdir_x(argument2, t);
py = argument1 + lengthdir_y(argument2, t);
is meant to be.
nx = argument0 + lengthdir_x(argument2, t);
ny = argument1 + lengthdir_y(argument2, t);

Again, thanks for the help. ;)
 

jo-thijs

Member
Yup, my bad...
I should really start testing everything before I post again to avoid typos and such things.

EDIT:
Oh, and you're welcome of course! :)
 
Top