Some minor changes

This commit is contained in:
ventor3000 2013-07-26 10:49:22 +02:00
commit 854a000b44

View file

@ -35,10 +35,6 @@ import strutils
## var vec2:TVEctor2d=vec & m #concatenates vec with m and returns a new vector ## var vec2:TVEctor2d=vec & m #concatenates vec with m and returns a new vector
const const
DEG360* = PI * 2.0 DEG360* = PI * 2.0
## 360 degrees in radians. ## 360 degrees in radians.
@ -93,13 +89,13 @@ proc point2d*(x,y:float):TPoint2d {.noInit,inline.}
let let
IDMATRIX2D*:TMatrix2d=matrix2d(1.0,0.0,0.0,1.0,0.0,0.0) IDMATRIX*:TMatrix2d=matrix2d(1.0,0.0,0.0,1.0,0.0,0.0)
## Quick access to an identity matrix ## Quick access to an identity matrix
ORIGO2D*:TPoint2d=Point2d(0.0,0.0) ORIGO*:TPoint2d=Point2d(0.0,0.0)
## Quick acces to point (0,0) ## Quick acces to point (0,0)
XAXIS2D*:TVector2d=vector2d(1.0,0.0) XAXIS*:TVector2d=vector2d(1.0,0.0)
## Quick acces to an 2d x-axis unit vector ## Quick acces to an 2d x-axis unit vector
YAXIS2D*:TVector2d=vector2d(0.0,1.0) YAXIS*:TVector2d=vector2d(0.0,1.0)
## Quick acces to an 2d y-axis unit vector ## Quick acces to an 2d y-axis unit vector
@ -216,7 +212,7 @@ proc mirror*(v:TVector2d):TMatrix2d {.noInit.} =
sqd=nd*(sqx-sqy) sqd=nd*(sqx-sqy)
if nd==inf or nd==neginf: if nd==inf or nd==neginf:
return IDMATRIX2D #mirroring around a zero vector is arbitrary=>just use identity return IDMATRIX #mirroring around a zero vector is arbitrary=>just use identity
result.setElements( result.setElements(
sqd,xy2, sqd,xy2,
@ -235,7 +231,7 @@ proc mirror*(v:TVector2d,org:TPoint2d):TMatrix2d {.noInit.} =
sqd=nd*(sqx-sqy) sqd=nd*(sqx-sqy)
if nd==inf or nd==neginf: if nd==inf or nd==neginf:
return IDMATRIX2D #mirroring around a zero vector is arbitrary=>just use identity return IDMATRIX #mirroring around a zero vector is arbitrary=>just use identity
result.setElements( result.setElements(
sqd,xy2, sqd,xy2,
@ -314,7 +310,7 @@ proc `=~`*(m1,m2:TMatrix2d):bool=
proc isIdentity*(m:TMatrix2d,tol=1.0e-6):bool= proc isIdentity*(m:TMatrix2d,tol=1.0e-6):bool=
## Checks is a matrix is approximately an identity matrix, ## Checks is a matrix is approximately an identity matrix,
## using `tol` as tolerance for each element. ## using `tol` as tolerance for each element.
return equals(m,IDMATRIX2D,tol) return equals(m,IDMATRIX,tol)
@ -413,12 +409,8 @@ proc tryNormalize*(v:var TVector2d):bool=
if mag==0.0: if mag==0.0:
return false return false
let v.x/=mag
newx=v.x/mag v.y/=mag
newy=v.y/mag
v.x=newx
v.y=newy
return true return true
@ -819,19 +811,23 @@ proc radToDeg*(rad:float):float=
## converts `rad` radians to degrees ## converts `rad` radians to degrees
rad * RAD2DEGCONST rad * RAD2DEGCONST
proc bisect*(v1,v2:TVector2d):TVector2d {.noInit.}=
## Computes the bisector between v1 and v2 as a normalize vector
proc bisect*(v1,v2:TVector2d):tuple[vec:TVector2d,success:bool]= ## If one of the input vectors has zero length, a normalized verison
## Computes the bisector between v1 and v2 as a normalize vector `vec` ## of the other is returned.
## This can fail if any of `v1` or `v2` has zero length, in which ## If both input vectors has zer length, an arbitrary normalize vector
## case `success` is set to false. ## is returned.
let var
vmag1=v1.len vmag1=v1.len
vmag2=v2.len vmag2=v2.len
if vmag1==0.0 or vmag2==0.0: # zero length vector equals arbitrary vector, just change to magnitude to one to
result.success=false # avoid zero division
return if vmag1==0.0:
if vmag2==0: #both are zero length return any normalized vector
return XAXIS
vmag1=1.0
if vmag2==0.0: vmag2=1.0
let let
x1=v1.x/vmag1 x1=v1.x/vmag1
@ -839,12 +835,13 @@ proc bisect*(v1,v2:TVector2d):tuple[vec:TVector2d,success:bool]=
x2=v2.x/vmag2 x2=v2.x/vmag2
y2=v2.y/vmag2 y2=v2.y/vmag2
result.x=(x1 + x2) * 0.5
result.y=(y1 + y2) * 0.5
result=(vector2d((x1 + x2) * 0.5, (y1 + y2) * 0.5) , true) if not result.tryNormalize():
if not result.vec.tryNormalize():
# This can happen if vectors are colinear. In this special case # This can happen if vectors are colinear. In this special case
# there are actually two bisectors, we select just # there are actually two bisectors, we select just
# one of them (x1,y1 rotated 90 degrees). # one of them (x1,y1 rotated 90 degrees ccw).
result.vec=vector2d(y1,-x1) result.x = -y1
result.y = x1