Module: PseudoGame.graphics.Polygon

Class that represents a simple convex 2D Polygon

Functions

PseudoGame.graphics.Polygon:new (vertices, colors) The constructor for a Polygon
PseudoGame.graphics.Polygon:vertex_color_pairs () Iterate over vertices and colors
PseudoGame.graphics.Polygon:edge_color_pairs () Iterate over edges and their colors
PseudoGame.graphics.Polygon:add_vertex (x, y[, r=0[, g=0[, b=0[, a=0]]]]) adds a vertex with a vertex color to the polygon
PseudoGame.graphics.Polygon:remove_vertex (index) removes a vertex and and its color from the polygon
PseudoGame.graphics.Polygon:set_vertex_pos (index, x, y) sets the position of a vertex
PseudoGame.graphics.Polygon:get_vertex_pos (index) gets a vertex position
PseudoGame.graphics.Polygon:set_vertex_color (index, r, g, b, a) sets the color of a vertex
PseudoGame.graphics.Polygon:get_vertex_color (index) gets a vertex color
PseudoGame.graphics.Polygon:resize (size) creates or removes vertices until the given size (vertex count) is reached new vertices are initialized at (0, 0) with the color black
PseudoGame.graphics.Polygon:is_clockwise () checks if the polygon is defined in clockwise order
PseudoGame.graphics.Polygon:clip (clipper_polygon[, generator]) Implementation of the sutherland hodgman algorithm for polygon clipping
PseudoGame.graphics.Polygon:slice (x0, y0, x1, y1, left, right[, generator_left[, generator_right]]) slice the polygon into two polygons at a line given by two points
PseudoGame.graphics.Polygon:copy () copies the polygon (this function is bad for performance if used a lot)
PseudoGame.graphics.Polygon:copy_data (polygon) copy the vertex and color data of another polygon onto this one
PseudoGame.graphics.Polygon:copy_data_transformed (polygon, transform_func) copy the vertex and color data of another polygon onto this one after transforming it
PseudoGame.graphics.Polygon:transform (transform_func) transform the vertices and vertex colors of the polygon

Fields

pseudogame.graphics.polygon.vertex_count


Functions

🔗 PseudoGame.graphics.Polygon:new (vertices, colors)
The constructor for a Polygon

Parameters:

Name Type(s) Description
vertices table the polygon's vertices in this format: {x0, y0, x1, y1, ...}
colors table the vertex colors in this format: {r0, g0, b0, a0, r1, g1, b1, a1, ...}

Returns:

    Polygon
🔗 PseudoGame.graphics.Polygon:vertex_color_pairs ()
Iterate over vertices and colors

Usage:

    for index, x, y, r, g, b, a in mypolygon:vertex_color_pairs() do
       ...
    end
🔗 PseudoGame.graphics.Polygon:edge_color_pairs ()
Iterate over edges and their colors

Usage:

    for x0, y0, r0, g0, b0, a0, x1, y1, r1, g1, b1, a1 in mypolygon:edge_color_pairs() do
       ...
    end
🔗 PseudoGame.graphics.Polygon:add_vertex (x, y[, r=0[, g=0[, b=0[, a=0]]]])
adds a vertex with a vertex color to the polygon

Parameters:

Name Type(s) Description Default value
x number the x coordinate of the vertex Not applicable
y number the y coordinate of the vertex Not applicable
r Optional number the r component of the vertex color 0
g Optional number the g component of the vertex color 0
b Optional number the b component of the vertex color 0
a Optional number the a component of the vertex color 0
🔗 PseudoGame.graphics.Polygon:remove_vertex (index)
removes a vertex and and its color from the polygon

Parameters:

Name Type(s) Description
index number the index of the vertex
🔗 PseudoGame.graphics.Polygon:set_vertex_pos (index, x, y)
sets the position of a vertex

Parameters:

Name Type(s) Description
index number the index of the vertex
x number the x coordinate the vertex should be set to
y number the y coordinate the vertex should be set to
🔗 PseudoGame.graphics.Polygon:get_vertex_pos (index)
gets a vertex position

Parameters:

Name Type(s) Description
index number the index of the vertex

Returns:

    number,number the position of the vertex
🔗 PseudoGame.graphics.Polygon:set_vertex_color (index, r, g, b, a)
sets the color of a vertex

Parameters:

Name Type(s) Description
index number the index of the vertex
r number the r component of the vertex color
g number the g component of the vertex color
b number the b component of the vertex color
a number the a component of the vertex color
🔗 PseudoGame.graphics.Polygon:get_vertex_color (index)
gets a vertex color

Parameters:

Name Type(s) Description
index number the index of the vertex

Returns:

    number,number,number,number the color of the vertex
🔗 PseudoGame.graphics.Polygon:resize (size)
creates or removes vertices until the given size (vertex count) is reached new vertices are initialized at (0, 0) with the color black

Parameters:

Name Type(s) Description
size number the vertex count the polygon will have after the operation
🔗 PseudoGame.graphics.Polygon:is_clockwise ()
checks if the polygon is defined in clockwise order

Returns:

    optional bool returns nil if order is undefined (polygon with no area)
🔗 PseudoGame.graphics.Polygon:clip (clipper_polygon[, generator])
Implementation of the sutherland hodgman algorithm for polygon clipping

Parameters:

Name Type(s) Description
clipper_polygon Polygon the polygon that will contain the newly created clipped polygon
generator Optional function use a generator instead of always creating new polygons (will create a polygon for every edge of the clipper polygon, so don't render this collection, this is just for improved performance)

Returns:

    optional Polygon Returns the clipped polygon or nil if no intersecting area exists
🔗 PseudoGame.graphics.Polygon:slice (x0, y0, x1, y1, left, right[, generator_left[, generator_right]])
slice the polygon into two polygons at a line given by two points

Parameters:

Name Type(s) Description
x0 number the x coordinate of the first point
y0 number the y coordinate of the first point
x1 number the x coordinate of the second point
y1 number the y coordinate of the second point
left bool specifies if the part of the polygon on the left side of the line should be returned
right bool specifies if the part of the polygon on the right side of the line should be returned
generator_left Optional function use a generator instead of creating new left polygons (this is good for performance)
generator_right Optional function use a generator instead of creating new right polygons (this is good for performance)

Returns:

    Polygon,Polygon, Polygon or nil returns either no, one or two polygons depending on left/right (returns nil when no part of the polygon is on a return side)
🔗 PseudoGame.graphics.Polygon:copy ()
copies the polygon (this function is bad for performance if used a lot)

Returns:

    Polygon
🔗 PseudoGame.graphics.Polygon:copy_data (polygon)
copy the vertex and color data of another polygon onto this one

Parameters:

Name Type(s) Description
polygon Polygon the polygon to copy data from
🔗 PseudoGame.graphics.Polygon:copy_data_transformed (polygon, transform_func)
copy the vertex and color data of another polygon onto this one after transforming it

Parameters:

Name Type(s) Description
polygon Polygon the polygon to copy data from
transform_func function a function that takes x, y, r, g, b, a and returns x, y, r, g, b, a
🔗 PseudoGame.graphics.Polygon:transform (transform_func)
transform the vertices and vertex colors of the polygon

Parameters:

Name Type(s) Description
transform_func function a function that takes x, y, r, g, b, a and returns x, y, r, g, b, a

Returns:

    Polygon returns itself for convenient chaining of operations

Fields

🔗 pseudogame.graphics.polygon.vertex_count
Name Type(s) Description
vertex_count number the amount of vertexes the polygon has
generated by LDoc 1.5.0