From db9fbbd8652fb4049c21a28a330893a94a5590ff Mon Sep 17 00:00:00 2001 From: Hugh Sanderson Date: Thu, 29 Feb 2024 15:34:25 +0800 Subject: [PATCH] Add normalized geometry functions --- src/nme/Assets.hx | 1 + src/nme/geom/Point.hx | 27 +++++++++++++++++++++++++-- src/nme/geom/Vector3D.hx | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/nme/Assets.hx b/src/nme/Assets.hx index 579eee269..442916254 100644 --- a/src/nme/Assets.hx +++ b/src/nme/Assets.hx @@ -489,6 +489,7 @@ class Assets i.isResource ? new Font("",null,null,i.path,id) : new Font(i.path,null,null,null,id); #end +trace('$id -> $font'); trySetCache(i,useCache,font); return font; diff --git a/src/nme/geom/Point.hx b/src/nme/geom/Point.hx index 0e3a1784c..290b005c0 100644 --- a/src/nme/geom/Point.hx +++ b/src/nme/geom/Point.hx @@ -78,8 +78,8 @@ class Point if (x == 0 && y == 0) { return; - - } else + } + else { var norm = thickness / Math.sqrt(x * x + y * y); x *= norm; @@ -87,6 +87,29 @@ class Point } } + inline public function dot(inPoint:Point):Float + { + return x*inPoint.x + y*inPoint.y; + } + + inline public function normalized(inplace=false):Point + { + var result = this; + var len = x*x+y*y; + var scale = Math.abs(len)>1e-7 ? 1.0/len : 0.0; + if (inplace) + { + x *= scale; + y *= scale; + } + else + { + result = new Point(x*scale, y*scale); + } + return result; + } + + public function offset(dx:Float, dy:Float):Void { x += dx; diff --git a/src/nme/geom/Vector3D.hx b/src/nme/geom/Vector3D.hx index 392258554..a705215e2 100644 --- a/src/nme/geom/Vector3D.hx +++ b/src/nme/geom/Vector3D.hx @@ -126,6 +126,27 @@ class Vector3D return l; } + inline public function normalized(inplace=false):Vector3D + { + var l = length; + var invL = l==0 ? 1 : 1.0/l; + var result = this; + if (inplace) + { + x *= invL; + y *= invL; + z *= invL; + } + else + { + result = new Vector3D(x*invL, y*invL, z*invL); + } + + return result; + } + + + inline public function project():Vector3D { x /= w;