GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2019-02-03 02:35:21

w0rm
Member
Registered: 2019-02-03
Posts: 1

Circles - Draws circles, ellipses, arcs and pies.

I needed a function to draw circle pies for my project. I noticed that the existing draw_pie script doesn't allow drawing of full circle so I modified the circle code from QB64 Wiki to work with GMS.

Expand/// @function scr_circles(cx,cy,r,color,alpha,sa,ea,aspect,outline,arc,step)
/// $description Draws circles, ellipses, arcs and pies.
///
/// @param {real} cx center X coordinate of circle
/// @param {real} cy center Y coordinate of circle
/// @param {real} r the radius of the circle
/// @param {color} color the circle's color
/// @param {real} alpha the alpha to be used when drawing [0..1]
/// @param {real} sa the angle in degrees on circle circumference to begin drawing at
/// @param {real} ea the angle in degrees on circle circumference to end drawing at
/// @param {real} aspect the aspect ratio of the circle
/// @param {bool} outline true to draw as an outline
/// @param {bool} arc true to draw as a arc (draws circle segment if both outline and arc are true)
/// @param {real} step step in degrees between points on circle circumference
///
/// Modified from https://qb64.org/wiki/Alternative_circle_routine
/// GMLscripts.com/license
{
	// Parameters
	var cx = argument0;
	var cy = argument1;
	var r  = argument2;
	var color  = argument3;
	var alpha  = argument4;
	var sa = argument5;
	var ea = argument6;
	var aspect  = argument7;
	var outline = argument8;
	var arc = argument9;
	var step = argument10;
	
	// Local variables used
	var nx;
	var ny;
	var xr;
	var yr;
	var st;
	var en;
	var stepp;
	var c;
	
	st = degtorad(sa); // convert start angle to radian and copy to local variable
	en = degtorad(ea); // convert end angle to radian and copy to local variable
	
	if aspect <= 0 // keep aspect ratio between 0 and 4
		aspect = 1;  
	if aspect > 4
		aspect = 4;
	
	if aspect < 1 // calculate x/y radius based on aspect ratio
		xr = r * aspect * 4;
	else
		xr = r;
		
	if aspect > 1
		yr = r * aspect;
	else
		yr = r;

	// Set color
	draw_set_color(color);
	draw_set_alpha(alpha);
	
	// Set drawing only outline or filled circle
	if (outline) draw_primitive_begin(pr_linestrip);
	else draw_primitive_begin(pr_trianglefan);
	
	if arc == false // draw line from center to start radian?
	{
		draw_vertex(cx,cy);
	
		nx = cx + xr * cos(st) // yes, compute starting point on circle's circumference
		ny = cy + yr * -sin(st)
		draw_vertex(nx, ny); // draw line from center to radian
	}

	if en <= st
		en = en + 6.2831852; // come back around to proper location (draw counterclockwise)

	stepp = degtorad(step);
	c = st; // cycle from start radian to end radian
	
	do
	{
		nx = cx + xr * cos(c); // compute next point on circle's circumference
		ny = cy + yr * -sin(c);
		draw_vertex(nx, ny);
		c = c + stepp;
	} until( c >= en );
	
	// compute last point on circle's circumference
	nx = cx + xr * cos(en);
	ny = cy + yr * -sin(en);
	draw_vertex(nx, ny);
		
	if arc == false // draw line from end radian to center?
		draw_vertex(cx, cy);

	draw_primitive_end();
	
	return 0;
}

Offline

Board footer

Powered by FluxBB