Skip to content

Commit efdab0a

Browse files
committed
docs + Rect.to_polygon()
1 parent 9d5d7a8 commit efdab0a

File tree

4 files changed

+90
-21
lines changed

4 files changed

+90
-21
lines changed

echo/shape/Circle.hx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ class Circle extends Shape implements IPooled {
1616
public var radius:Float;
1717
public var diameter(get, set):Float;
1818
public var pooled:Bool;
19-
19+
/**
20+
* Gets a Cirlce from the pool, or creates a new one if none are available. Call `put()` on the Cirlce to place it back in the pool.
21+
* @param x
22+
* @param y
23+
* @param radius
24+
* @param rotation
25+
* @return Circle
26+
*/
2027
public static inline function get(x:Float = 0, y:Float = 0, radius:Float = 1, rotation:Float = 0):Circle {
2128
var circle = _pool.get();
2229
circle.set(x, y, radius, rotation);

echo/shape/Polygon.hx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ class Polygon extends Shape implements IPooled {
3838
var local_frame:Frame2;
3939

4040
var local_vertices:Array<Vector2>;
41-
41+
/**
42+
* Gets a Polygon from the pool, or creates a new one if none are available. Call `put()` on the Polygon to place it back in the pool.
43+
* @param x
44+
* @param y
45+
* @param sides
46+
* @param radius
47+
* @param rotation
48+
* @return Polygon
49+
*/
4250
public static inline function get(x:Float = 0, y:Float = 0, sides:Int = 3, radius:Float = 1, rotation:Float = 0):Polygon {
4351
if (sides < 3) throw 'Polygons require 3 sides as a minimum';
4452

@@ -58,14 +66,25 @@ class Polygon extends Shape implements IPooled {
5866
polygon.pooled = false;
5967
return polygon;
6068
}
61-
69+
/**
70+
* Gets a Polygon from the pool, or creates a new one if none are available. Call `put()` on the Polygon to place it back in the pool.
71+
* @param x
72+
* @param y
73+
* @param rotation
74+
* @param vertices
75+
* @return Polygon
76+
*/
6277
public static inline function get_from_vertices(x:Float = 0, y:Float = 0, rotation:Float = 0, ?vertices:Array<Vector2>):Polygon {
6378
var polygon = _pool.get();
6479
polygon.set(x, y, vertices);
6580
polygon.pooled = false;
6681
return polygon;
6782
}
68-
83+
/**
84+
* Gets a Polygon from the pool, or creates a new one if none are available. Call `put()` on the Polygon to place it back in the pool.
85+
* @param rect
86+
* @return Polygon return _pool.get().set_from_rect(rect)
87+
*/
6988
public static inline function get_from_rect(rect:Rect):Polygon return _pool.get().set_from_rect(rect);
7089

7190
// TODO

echo/shape/Rect.hx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,31 @@ class Rect extends Shape implements IPooled {
4242
public var pooled:Bool;
4343

4444
public var transformed_rect(default, null):Null<Polygon>;
45-
45+
/**
46+
* Gets a Rect from the pool, or creates a new one if none are available. Call `put()` on the Rect to place it back in the pool.
47+
*
48+
* Note - The X and Y positions represent the center of the Rect. To set the Rect from its Top-Left origin, `Rect.get_from_min_max()` is available.
49+
* @param x The centered X position of the Rect.
50+
* @param y The centered Y position of the Rect.
51+
* @param width The width of the Rect.
52+
* @param height The height of the Rect.
53+
* @param rotation The rotation of the Rect.
54+
* @return Rect
55+
*/
4656
public static inline function get(x:Float = 0, y:Float = 0, width:Float = 1, height:Float = 0, rotation:Float = 0):Rect {
4757
var rect = _pool.get();
4858
rect.set(x, y, width, height, rotation);
4959
rect.pooled = false;
5060
return rect;
5161
}
52-
62+
/**
63+
* Gets a Rect from the pool, or creates a new one if none are available. Call `put()` on the Rect to place it back in the pool.
64+
* @param min_x
65+
* @param min_y
66+
* @param max_x
67+
* @param max_y
68+
* @return Rect
69+
*/
5370
public static inline function get_from_min_max(min_x:Float, min_y:Float, max_x:Float, max_y:Float):Rect {
5471
var rect = _pool.get();
5572
rect.set_from_min_max(min_x, min_y, max_x, max_y);
@@ -107,6 +124,15 @@ class Rect extends Shape implements IPooled {
107124
return bounds();
108125
}
109126

127+
public function to_polygon(put_self:Bool = false):Polygon {
128+
if (put_self) {
129+
var polygon = Polygon.get_from_rect(this);
130+
put();
131+
return polygon;
132+
}
133+
return Polygon.get_from_rect(this);
134+
}
135+
110136
override inline function bounds(?aabb:AABB):AABB {
111137
if (transformed_rect != null) return transformed_rect.bounds(aabb);
112138
return (aabb == null) ? AABB.get(x, y, width, height) : aabb.set(x, y, width, height);

echo/util/AABB.hx

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,36 @@ class AABB implements IPooled {
1616
public var height(get, never):Float;
1717

1818
public var pooled:Bool;
19-
19+
/**
20+
* Gets an AABB from the pool, or creates a new one if none are available. Call `put()` on the AABB to place it back in the pool.
21+
*
22+
* Note - The X and Y positions represent the center of the AABB. To set the AABB from its Top-Left origin, `AABB.get_from_min_max()` is available.
23+
* @param x The centered X position of the AABB.
24+
* @param y The centered Y position of the AABB.
25+
* @param width The width of the AABB.
26+
* @param height The height of the AABB.
27+
* @param rotation The rotation of the AABB.
28+
* @return AABB
29+
*/
2030
public static inline function get(x:Float = 0, y:Float = 0, width:Float = 1, height:Float = 0):AABB {
21-
var rect = _pool.get();
22-
rect.set(x, y, width, height);
23-
rect.pooled = false;
24-
return rect;
31+
var aabb = _pool.get();
32+
aabb.set(x, y, width, height);
33+
aabb.pooled = false;
34+
return aabb;
2535
}
26-
36+
/**
37+
* Gets an AABB from the pool, or creates a new one if none are available. Call `put()` on the AABB to place it back in the pool.
38+
* @param min_x
39+
* @param min_y
40+
* @param max_x
41+
* @param max_y
42+
* @return AABB
43+
*/
2744
public static inline function get_from_min_max(min_x:Float, min_y:Float, max_x:Float, max_y:Float):AABB {
28-
var rect = _pool.get();
29-
rect.set_from_min_max(min_x, min_y, max_x, max_y);
30-
rect.pooled = false;
31-
return rect;
45+
var aabb = _pool.get();
46+
aabb.set_from_min_max(min_x, min_y, max_x, max_y);
47+
aabb.pooled = false;
48+
return aabb;
3249
}
3350

3451
inline function new() {
@@ -65,11 +82,11 @@ class AABB implements IPooled {
6582
return this.min_x < other.max_x && this.max_x >= other.min_x && this.min_y < other.max_y && this.max_y >= other.min_y;
6683
}
6784

68-
public inline function load(rect:AABB) {
69-
this.min_x = rect.min_x;
70-
this.max_x = rect.max_x;
71-
this.min_y = rect.min_y;
72-
this.max_y = rect.max_y;
85+
public inline function load(aabb:AABB) {
86+
this.min_x = aabb.min_x;
87+
this.max_x = aabb.max_x;
88+
this.min_y = aabb.min_y;
89+
this.max_y = aabb.max_y;
7390
}
7491

7592
public function put() {

0 commit comments

Comments
 (0)