From 496c73a50f0f396192e98da8b513fdbe24a2fd75 Mon Sep 17 00:00:00 2001 From: Maikel de Vries Date: Sat, 13 May 2017 14:59:14 +0200 Subject: [PATCH] add a method to calculate the area to the shape library --- docs/sdk/script/Shape.xml | 3 ++ .../Libraries.ocd/Shape.ocd/Script.c | 39 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/sdk/script/Shape.xml b/docs/sdk/script/Shape.xml index 09be43d48..2668eccc4 100644 --- a/docs/sdk/script/Shape.xml +++ b/docs/sdk/script/Shape.xml @@ -38,6 +38,9 @@ Finds a random position in the shape and returns it as properties x and y in the supplied proplist. The return value indivates whether a point could be found. The parameter max_tries indicates how many times the algorithm tries to find a point within the shape. The function is guaranteed to succeed for non-empty base shapes (i.e. rectangle and circle) as well as combined shapes on a single try. However a stochastic approach is used for intersection and subtraction shapes where random points are queried from one of the sub-shapes and subsequently checked against the other sub-shapes. + bool GetArea(); + Returns the area covered by the shape in squared pixels. + proplist GetBoundingRectangle(); Returns a rectangular shape that includes at least the whole shape used as calling context. diff --git a/planet/Objects.ocd/Libraries.ocd/Shape.ocd/Script.c b/planet/Objects.ocd/Libraries.ocd/Shape.ocd/Script.c index fbbf0df62..88a551019 100644 --- a/planet/Objects.ocd/Libraries.ocd/Shape.ocd/Script.c +++ b/planet/Objects.ocd/Libraries.ocd/Shape.ocd/Script.c @@ -10,6 +10,18 @@ local BaseShape; +// Returns the area in squared pixels covered by the shape. Works for all specific shapes by using their functionality. +public func GetArea() +{ + var check_rect = this->GetBoundingRectangle(); + var area = 0; + for (var x = check_rect.x; x < check_rect.x + check_rect.wdt; x++) + for (var y = check_rect.y; y < check_rect.y + check_rect.hgt; y++) + if (this->IsPointContained(x, y)) + area++; + return area; +} + /* Rectangle shape */ local BaseRectangle; // properties x,y,w,h @@ -44,6 +56,11 @@ private func BaseRectangle_GetRandomPoint(proplist result) return true; } +private func BaseRectangle_GetArea() +{ + return this.wdt * this.hgt; +} + private func BaseRectangle_ToString() { return Format("Shape->Rectangle(%d, %d, %d, %d)", this.x, this.y, this.wdt, this.hgt); @@ -69,9 +86,9 @@ public func Rectangle(int x, int y, int w, int h) /* Circle shape */ -local BaseCircle; // properties cx,cy,xr,yr +local BaseCircle; // properties cx,cy,r -// point contained in rectangle? +// point contained in circle? private func BaseCircle_IsPointContained(int x, int y) { x-=this.cx; y-=this.cy; @@ -100,6 +117,17 @@ private func BaseCircle_GetRandomPoint(proplist result) return true; } +public func BaseCircle_GetArea() +{ + // Is aleady covered by general method, but this direct calculation for a simple circle is faster. + var area = 0; + for (var x = 0; x <= this.r; x++) + for (var y = 1; y <= this.r; y++) + if (x*x + y*y <= this.r*this.r) + area++; + return 4 * area + 1; +} + /** Constructor of circular area. @par cx Global center x of circle @par cy Global center y of circle @@ -315,13 +343,17 @@ public func Subtract(proplist in, proplist ex) public func Definition(def) { // Initialize function pointers in shape classes - BaseShape = {}; + BaseShape = + { + GetArea = Shape.GetArea + }; BaseRectangle = new BaseShape { Type = "rect", IsPointContained = Shape.BaseRectangle_IsPointContained, GetBoundingRectangle = Shape.BaseRectangle_GetBoundingRectangle, GetRandomPoint = Shape.BaseRectangle_GetRandomPoint, + GetArea = Shape.BaseRectangle_GetArea, Find_In = Shape.BaseRectangle_Find_In, Find_At = Shape.BaseRectangle_Find_At, ToString = Shape.BaseRectangle_ToString, @@ -332,6 +364,7 @@ public func Definition(def) IsPointContained = Shape.BaseCircle_IsPointContained, GetBoundingRectangle = Shape.BaseCircle_GetBoundingRectangle, GetRandomPoint = Shape.BaseCircle_GetRandomPoint, + GetArea = Shape.BaseCircle_GetArea }; BaseIntersection = new BaseShape {