/// @func draw_line_advanced()
/// @param {real} x1 The x coordinate of the start of the line.
/// @param {real} y1 The y coordinate of the start of the line.
/// @param {real} x2 The x coordinate of the end of the line.
/// @param {real} y2 The y coordinate of the end of the line.
/// @param {integer} width The width in pixels of the line.
/// @param {integer} color The color of the line.
function draw_line_advanced(_x1, _y1, _x2, _y2, _width, _color) {
// Zero width case
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
draw_vertex_color(_x1, _y1, _color, 1);
draw_vertex_color(_x2, _y2, _color, 1);
draw_primitive_end();
exit;
}
// Default case
var _nX = _y2 -_y1;
var _nY = _x1 -_x2;
var _d = sqrt(_nX*_nX +_nY*_nY);
_nX = _nX*_width/_d/2;
_nY = _nY*_width/_d/2;
draw_primitive_begin(pr_trianglestrip);
draw_vertex_color(_x1 +_nX, _y1 +_nY, _color, 1);
draw_vertex_color(_x1 -_nX, _y1 -_nY, _color, 1);
draw_vertex_color(_x2 +_nX, _y2 +_nY, _color, 1);
draw_vertex_color(_x2 -_nX, _y2 -_nY, _color, 1);
draw_primitive_end();
}
/// @func draw_arc_advanced()
/// @param {real} x The x coordinate of the center of the arc.
/// @param {real} y The y coordinate of the center of the arc.
/// @param {real} r The arc's radius (length from its center to its edge).
/// @param {real} angle The starting angle in degrees.
/// @param {real} span The arc's span in degrees.
/// @param {integer} width The width in pixels of the arc.
/// @param {integer} color The color of the arc.
function draw_arc_advanced(_x, _y, _r, _angle, _span, _width, _color) {
var _increment = 15*sign(_span);
var _traversal = 0;
// Zero width case
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
// Draw vertices up to the last one
while (abs(_traversal) < abs(_span)) {
var _dcos = dcos(_angle +_traversal);
var _dsin = dsin(_angle +_traversal);
draw_vertex_color(_x +_r*_dcos, _y -_r*_dsin, _color, 1);
_traversal += _increment;
}
// Draw last vertex
var _dcos = dcos(_angle +_span);
var _dsin = dsin(_angle +_span);
draw_vertex_color(_x +_r*_dcos, _y -_r*_dsin, _color, 1);
draw_primitive_end();
exit;
}
// Default case
var _ro = _r +_width/2;
var _ri = _r -_width/2;
draw_primitive_begin(pr_trianglestrip);
// Draw vertices up to the last one
while (abs(_traversal) < abs(_span)) {
var _dcos = dcos(_angle +_traversal);
var _dsin = dsin(_angle +_traversal);
draw_vertex_color(_x +_ro*_dcos, _y -_ro*_dsin, _color, 1);
draw_vertex_color(_x +_ri*_dcos, _y -_ri*_dsin, _color, 1);
_traversal += _increment;
}
// Draw last vertex
var _dcos = dcos(_angle +_span);
var _dsin = dsin(_angle +_span);
draw_vertex_color(_x +_ro*_dcos, _y -_ro*_dsin, _color, 1);
draw_vertex_color(_x +_ri*_dcos, _y -_ri*_dsin, _color, 1);
draw_primitive_end();
}
/// @func draw_rectangle_advanced()
/// @param {real} x1 The x coordinate of the left of the rectangle.
/// @param {real} y1 The y coordinate of the top of the rectangle.
/// @param {real} x2 The x coordinate of the right of the rectangle.
/// @param {real} y2 The y coordinate of the bottom of the rectangle.
/// @param {integer} color The color of the rectangle.
/// @param {boolean} outline Whether the rectangle is drawn filled (false) or as an outline (true).
/// @param {integer} width The width of the outline in pixels. Ignored of filled.
function draw_rectangle_advanced(_x1, _y1, _x2, _y2, _color, _outline, _width) {
// Filled case
if (!_outline) {
draw_primitive_begin(pr_trianglestrip);
draw_vertex_color(_x1, _y1, _color, 1);
draw_vertex_color(_x2, _y1, _color, 1);
draw_vertex_color(_x1, _y2, _color, 1);
draw_vertex_color(_x2, _y2, _color, 1);
draw_primitive_end();
exit;
}
// Outline with 0 width
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
draw_vertex_color(_x1, _y1, _color, 1);
draw_vertex_color(_x2, _y1, _color, 1);
draw_vertex_color(_x2, _y2, _color, 1);
draw_vertex_color(_x1, _y2, _color, 1);
draw_vertex_color(_x1, _y1, _color, 1);
draw_primitive_end();
exit;
}
// Outline with width
_width /= 2;
draw_primitive_begin(pr_trianglestrip);
// Top left corner
draw_vertex_color(_x1 -_width, _y1 -_width, _color, 1);
draw_vertex_color(_x1 +_width, _y1 +_width, _color, 1);
// Top right corner
draw_vertex_color(_x2 +_width, _y1 -_width, _color, 1);
draw_vertex_color(_x2 -_width, _y1 +_width, _color, 1);
// Bottom right corner
draw_vertex_color(_x2 +_width, _y2 +_width, _color, 1);
draw_vertex_color(_x2 -_width, _y2 -_width, _color, 1);
// Bottom left corner
draw_vertex_color(_x1 -_width, _y2 +_width, _color, 1);
draw_vertex_color(_x1 +_width, _y2 -_width, _color, 1);
// Top left corner
draw_vertex_color(_x1 -_width, _y1 -_width, _color, 1);
draw_vertex_color(_x1 +_width, _y1 +_width, _color, 1);
draw_primitive_end();
}
/// @func draw_circle_advanced()
/// @param {real} x The x coordinate of the center of the circle.
/// @param {real} y The y coordinate of the center of the circle.
/// @param {real} r The radius (distance from center to edge) of the circle in pixels.
/// @param {integer} color The color of the circle.
/// @param {boolean} outline Whether the circle is an outline (true) or not (false).
/// @param {integer} width The width of the outline in pixels. Ignored of filled.
function draw_circle_advanced(_x, _y, _r, _color, _outline, _width) {
// Filled case
if (!_outline) {
draw_primitive_begin(pr_trianglefan);
draw_vertex_color(_x, _y, _color, 1);
var _a = 360;
repeat (360/15 +1) {draw_vertex_color(_x +_r*dcos(_a), _y +_r*dsin(_a), _color, 1); _a -= 15;}
draw_primitive_end();
exit;
}
// Outline with 0 width
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
var _a = 360;
repeat (360/15 +1) {draw_vertex_color(_x +_r*dcos(_a), _y +_r*dsin(_a), _color, 1); _a -= 15;}
draw_primitive_end();
exit;
}
// Outline with width
var _ro = _r +_width/2;
var _ri = _r -_width/2;
draw_primitive_begin(pr_trianglestrip);
var _a = 360;
repeat (360/15 +1) {
var _cos = dcos(_a);
var _sin = dsin(_a);
draw_vertex_color(_x +_ro*_cos, _y +_ro*_sin, _color, 1);
draw_vertex_color(_x +_ri*_cos, _y +_ri*_sin, _color, 1);
_a -= 15;
}
draw_primitive_end();
}
/// @func draw_capsule_advanced()
/// @param {real} x1 The x coordinate of the center of the start of the capsule.
/// @param {real} y1 The y coordinate of the center of the start of the capsule.
/// @param {real} x2 The x coordinate of the center of the end of the capsule.
/// @param {real} y2 The y coordinate of the center of the end of the capsule.
/// @param {real} r The radius (distance from center to edge) of the capsule in pixels.
/// @param {integer} color The color of the capsule.
/// @param {boolean} outline Whether the capsule is drawn filled (false) or as an outline (true).
/// @param {integer} width The width of the outline (in pixels). Ignored of filled.
function draw_capsule_advanced(_x1, _y1, _x2, _y2, _r, _color, _outline, _width) {
// Normal vector
var _d = sqrt((_y2 -_y1)*(_y2 -_y1) +(_x1 -_x2)*(_x1 -_x2));
var _vx = (_y2 -_y1)/_d*_r;
var _vy = (_x1 -_x2)/_d*_r;
var _a = darctan2(_vy, _vx);
// Filled case
if (!_outline) {
draw_primitive_begin(pr_trianglefan);
draw_vertex_color((_x1 +_x2)/2, (_y1 +_y2)/2, _color, 1);
repeat (180/15 +1) {draw_vertex_color(_x1 +_r*dcos(_a), _y1 +_r*dsin(_a), _color, 1); _a -= 15;}
_a += 15;
repeat (180/15 +1) {draw_vertex_color(_x2 +_r*dcos(_a), _y2 +_r*dsin(_a), _color, 1); _a -= 15;}
draw_vertex_color(_x1 +_vx, _y1 +_vy, _color, 1);
draw_primitive_end();
exit;
}
// Outline with 0 width
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
repeat (180/15 +1) {draw_vertex_color(_x1 +_r*dcos(_a), _y1 +_r*dsin(_a), _color, 1); _a -= 15;}
_a += 15;
repeat (180/15 +1) {draw_vertex_color(_x2 +_r*dcos(_a), _y2 +_r*dsin(_a), _color, 1); _a -= 15;}
draw_vertex_color(_x1 +_vx, _y1 +_vy, _color, 1);
draw_primitive_end();
exit;
}
// Outline with width
var _ro = _r +_width/2;
var _ri = _r -_width/2;
draw_primitive_begin(pr_trianglestrip);
repeat (180/15 +1) {
var _cos = dcos(_a);
var _sin = dsin(_a);
draw_vertex_color(_x1 +_ro*_cos, _y1 +_ro*_sin, _color, 1);
draw_vertex_color(_x1 +_ri*_cos, _y1 +_ri*_sin, _color, 1);
_a -= 15;
}
_a += 15;
repeat (180/15 +1) {
var _cos = dcos(_a);
var _sin = dsin(_a);
draw_vertex_color(_x2 +_ro*_cos, _y2 +_ro*_sin, _color, 1);
draw_vertex_color(_x2 +_ri*_cos, _y2 +_ri*_sin, _color, 1);
_a -= 15;
}
draw_vertex_color(_x1 +_ro*_cos, _y1 +_ro*_sin, _color, 1);
draw_vertex_color(_x1 +_ri*_cos, _y1 +_ri*_sin, _color, 1);
draw_primitive_end();
}
/// @func draw_polygon_advanced()
/// @param {array} polygon A closed convex polygon array structure [x1, y1, x2, y2, x3, y3,...].
/// @param {integer} color The color of the polygon.
/// @param {boolean} outline Whether the polygon is drawn filled (false) or as an outline (true).
/// @param {integer} width The width of the outline (in pixels). Ignored of filled.
function draw_polygon_advanced(_polygon, _color, _outline, _width) {
var _vertexCount = array_length(_polygon)/2;
var _i = 0;
// Filled case
if (!_outline) {
draw_primitive_begin(pr_trianglefan);
repeat (_vertexCount) {draw_vertex_color(_polygon[_i*2], _polygon[_i*2 +1], _color, 1); ++_i;}
draw_primitive_end();
exit;
}
// Outline with 0 width
if (_width <= 0) {
draw_primitive_begin(pr_linestrip);
repeat (_vertexCount) {draw_vertex_color(_polygon[_i*2], _polygon[_i*2 +1], _color, 1); ++_i;}
draw_vertex_color(_polygon[0], _polygon[1], _color, 1);
draw_primitive_end();
exit;
}
// Outline with width
var _m = array_create(_vertexCount); // Slopes
var _bi = array_create(_vertexCount); // Inner edge intercepts
var _bo = array_create(_vertexCount); // Outer edge intercepts
// Loop to find the slope and intercepts of the inner and outer edges
repeat (_vertexCount) {
var _j = (_i +1) % _vertexCount;
var _vx = _polygon[_j*2] -_polygon[_i*2];
var _vy = _polygon[_j*2 +1] -_polygon[_i*2 +1];
var _d = sqrt(_vx*_vx +_vy*_vy);
var _nx = _vy*_width/2/_d;
var _ny = -_vx*_width/2/_d;
_m[_i] = _vy/_vx;
_bo[_i] = (_polygon[_i*2 +1] +_ny) -(_polygon[_i*2] +_nx)*_m[_i];
_bi[_i] = (_polygon[_i*2 +1] -_ny) -(_polygon[_i*2] -_nx)*_m[_i];
++_i;
}
// Draw loop
var _i = 0;
draw_primitive_begin(pr_trianglestrip);
repeat (_vertexCount +1) {
var _j = (_i +1) % _vertexCount;
if (abs(_m[_i]) == infinity) {
var _ox = _polygon[_i*2] +_nx;
var _oy = _m[_j]*_ox +_bo[_j];
var _ix = _polygon[_i*2] -_nx;
var _iy = _m[_j]*_ix +_bi[_j];
}
else if (abs(_m[_j]) == infinity) {
var _ox = _polygon[_j*2] +_nx;
var _oy = _m[_i]*_ox +_bo[_i];
var _ix = _polygon[_j*2] -_nx;
var _iy = _m[_i]*_ix +_bi[_i];
}
else {
var _ox = (_bo[_j] -_bo[_i])/(_m[_i] -_m[_j]);
var _oy = _m[_i]*_ox +_bo[_i];
var _ix = (_bi[_j] -_bi[_i])/(_m[_i] -_m[_j]);
var _iy = _m[_i]*_ix +_bi[_i];
}
draw_vertex_color(_ox, _oy, _color, 1);
draw_vertex_color(_ix, _iy, _color, 1);
_i = (_i +1) % _vertexCount;
}
draw_primitive_end();
}