Skip to content

Commit

Permalink
Add normalized geometry functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Feb 29, 2024
1 parent fc96564 commit db9fbbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/nme/Assets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 25 additions & 2 deletions src/nme/geom/Point.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,38 @@ class Point
if (x == 0 && y == 0)
{
return;

} else
}
else
{
var norm = thickness / Math.sqrt(x * x + y * y);
x *= norm;
y *= norm;
}
}

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;
Expand Down
21 changes: 21 additions & 0 deletions src/nme/geom/Vector3D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit db9fbbd

Please sign in to comment.