Also cleaned up PolygonBase.

This commit is contained in:
Relintai 2022-06-12 14:24:27 +02:00
parent a4276b11d0
commit 21ca852196
2 changed files with 121 additions and 136 deletions

View File

@ -1,147 +1,128 @@
#include "polygon_base.h"
#include "core/math/geometry.h"
PoolVector2Array PolygonBase::get_points() {
return points;
void PolygonBase::clear() {
points.resize(0);
_polygon_changed();
}
void PolygonBase::set_points(const PoolVector2Array &val) {
points = val;
void PolygonBase::add_point(const float x, const float y, const bool closed) {
Vector2 p = Vector2(x, y);
int points_count = points.size();
if (points_count < 3) {
points.append(p);
_polygon_changed();
return;
}
Vector2 segs[2];
segs[0] = points[0];
segs[1] = points[points_count - 1];
float min_length = (p - Geometry::get_closest_point_to_segment_2d(p, segs)).length();
int insert_point = 0;
for (int i = 0; i < points_count - 1; ++i) {
segs[0] = points[i];
segs[1] = points[i + 1];
float length = (p - Geometry::get_closest_point_to_segment_2d(p, segs)).length();
if (length < min_length) {
min_length = length;
insert_point = i + 1;
}
}
if (!closed && insert_point == 0 && (points[0] - p).length() > (points[points_count - 1] - p).length()) {
insert_point = points_count;
}
points.insert(insert_point, p);
_polygon_changed();
}
bool PolygonBase::remove_point(const int index) {
int s = points.size();
if (s < 4 || index < 0 || index >= s) {
return false;
} else {
points.remove(index);
_polygon_changed();
}
//tool;
//export(PoolVector2Array) ;
PoolVector2Array points = [Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)];
void PolygonBase::clear() {
points.resize(0);
_polygon_changed();
return true;
}
void PolygonBase::add_point(const float x, const float y, const bool closed) {
Vector2 p = Vector2(x, y);
Variant = points.size();
if (points_count < 3) {
points.append(p);
_polygon_changed();
return;
int PolygonBase::get_point_count() {
return points.size();
}
float min_length = (p-Geometry.get_closest_point_to_segment_2d(p, points[0], points[points_count-1])).length();
Variant = 0;
for (i in points_count-1) {
Variant = (p - Geometry.get_closest_point_to_segment_2d(p, points[i], points[i+1])).length();
if (length < min_length) {
min_length = length;
insert_point = i+1;
Vector2 PolygonBase::get_point(const int i) {
return points[i];
}
void PolygonBase::set_point(const int i, const Vector2 &v) {
points[i] = v;
_polygon_changed();
}
if (!closed && insert_point == 0 && (points[0]-p).length() > (points[points_count-1]-p).length()) {
insert_point = points_count;
void PolygonBase::set_points(const PoolVector2Array &v) {
points = v;
_polygon_changed();
}
points.insert(insert_point, p);
_polygon_changed();
void PolygonBase::polygon_changed() {
_polygon_changed();
}
bool PolygonBase::remove_point(const int index) {
Variant = points.size();
if (s < 4 || index < 0 || index >= s) {
return false;
void PolygonBase::_polygon_changed() {
emit_changed();
}
String PolygonBase::to_string() {
String rv;
else {
points.remove(index);
_polygon_changed();
PoolVector2Array ps = get_points();
for (int i = 0; i < ps.size(); ++i) {
Vector2 p = ps[i];
rv += "(" + String::num(p.x) + "," + String::num(p.y) + ")" + ",";
}
return rv;
}
return true;
PolygonBase::PolygonBase() {
points.push_back(Vector2(0.2, 0.2));
points.push_back(Vector2(0.7, 0.4));
points.push_back(Vector2(0.4, 0.7));
}
int PolygonBase::get_point_count() {
return points.size();
PolygonBase::~PolygonBase() {
}
void PolygonBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_points"), &PolygonBase::get_points);
ClassDB::bind_method(D_METHOD("set_points", "value"), &PolygonBase::set_points);
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
Vector2 PolygonBase::get_point(const int i) {
return points[i];
ClassDB::bind_method(D_METHOD("clear"), &PolygonBase::clear);
ClassDB::bind_method(D_METHOD("add_point", "x", "y", "closed"), &PolygonBase::add_point, true);
ClassDB::bind_method(D_METHOD("remove_point", "index"), &PolygonBase::remove_point);
ClassDB::bind_method(D_METHOD("get_point_count"), &PolygonBase::get_point_count);
ClassDB::bind_method(D_METHOD("get_point", "i"), &PolygonBase::get_point);
ClassDB::bind_method(D_METHOD("set_point", "i", "v"), &PolygonBase::set_point);
BIND_VMETHOD(MethodInfo("_polygon_changed"));
ClassDB::bind_method(D_METHOD("polygon_changed"), &PolygonBase::polygon_changed);
ClassDB::bind_method(D_METHOD("_polygon_changed"), &PolygonBase::_polygon_changed);
ClassDB::bind_method(D_METHOD("to_string"), &PolygonBase::to_string);
}
void PolygonBase::set_point(const int i, const Vector2 &v) {
points[i] = v;
_polygon_changed();
}
void PolygonBase::set_points(const PoolVector2Array &v) {
points = v;
_polygon_changed();
}
void PolygonBase::polygon_changed() {
_polygon_changed();
}
void PolygonBase::_polygon_changed() {
emit_changed();
}
String PolygonBase::to_string() {
Variant = PoolStringArray();
for (p in points) {
rv.append("("+str(p.x)+","+str(p.y)+")");
}
return rv.join(",");
}
}
PolygonBase::PolygonBase() {
points = [Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)];
}
PolygonBase::~PolygonBase() {
}
static void PolygonBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_points"), &PolygonBase::get_points);
ClassDB::bind_method(D_METHOD("set_points", "value"), &PolygonBase::set_points);
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
ClassDB::bind_method(D_METHOD("clear"), &PolygonBase::clear);
ClassDB::bind_method(D_METHOD("add_point", "x", "y", "closed"), &PolygonBase::add_point, true);
ClassDB::bind_method(D_METHOD("remove_point", "index"), &PolygonBase::remove_point);
ClassDB::bind_method(D_METHOD("get_point_count"), &PolygonBase::get_point_count);
ClassDB::bind_method(D_METHOD("get_point", "i"), &PolygonBase::get_point);
ClassDB::bind_method(D_METHOD("set_point", "i", "v"), &PolygonBase::set_point);
ClassDB::bind_method(D_METHOD("set_points", "v"), &PolygonBase::set_points);
ClassDB::bind_method(D_METHOD("polygon_changed"), &PolygonBase::polygon_changed);
ClassDB::bind_method(D_METHOD("_polygon_changed"), &PolygonBase::_polygon_changed);
ClassDB::bind_method(D_METHOD("to_string"), &PolygonBase::to_string);
}

View File

@ -1,36 +1,40 @@
#ifndef POLYGON_BASE_H
#define POLYGON_BASE_H
#include "core/math/vector2.h"
#include "core/variant.h"
#include "../mm_node.h"
class PolygonBase : public MMNode {
GDCLASS(PolygonBase, MMNode);
GDCLASS(PolygonBase, MMNode);
public:
public:
void clear();
PoolVector2Array get_points();
void set_points(const PoolVector2Array &val);
void add_point(const float x, const float y, const bool closed = true);
bool remove_point(const int index);
void clear();
void add_point(const float x, const float y, const bool closed = true);
bool remove_point(const int index);
int get_point_count();
Vector2 get_point(const int i);
void set_point(const int i, const Vector2 &v);
void set_points(const PoolVector2Array &v);
void polygon_changed();
void _polygon_changed();
String to_string();
int get_point_count();
Vector2 get_point(const int i);
PolygonBase();
~PolygonBase();
void set_point(const int i, const Vector2 &v);
protected:
static void _bind_methods();
PoolVector2Array get_points();
void set_points(const PoolVector2Array &v);
//tool
//export(PoolVector2Array)
PoolVector2Array points = [Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)];
void polygon_changed();
void _polygon_changed();
String to_string();
PolygonBase();
~PolygonBase();
protected:
static void _bind_methods();
PoolVector2Array points;
};
#endif