Vector.mesa
Last changed by Michael Plass, August 4, 1982 11:58 am
Written by Doug Wyatt, September 12, 1980 11:49 AM
DIRECTORY
GraphicsBasic USING [Vec],
RealFns USING[SqRt];
Vector: DEFINITIONS IMPORTS RealFns = {
Vec: TYPE = GraphicsBasic.Vec;
Matrix: TYPE = RECORD[a11,a12,a21,a22: REAL];
Vector operations
Add: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE {
RETURN[[a.x+b.x,a.y+b.y]]
};
Sub: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE {
RETURN[[a.x-b.x,a.y-b.y]]
};
Neg: PROC[a: Vec] RETURNS[Vec] = INLINE {
RETURN[[-a.x,-a.y]]
};
Mul: PROC[a: Vec, s: REAL] RETURNS[Vec] = INLINE {
RETURN[[a.x*s,a.y*s]]
};
Div: PROC[a: Vec, s: REAL] RETURNS[Vec] = INLINE {
RETURN[[a.x/s,a.y/s]]
};
Ls: PROC[a: Vec, b: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a.x<b.x AND a.y<b.y]
};
Le: PROC[a: Vec, b: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a.x<=b.x AND a.y<=b.y]
};
Ge: PROC[a: Vec, b: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a.x>=b.x AND a.y>=b.y]
};
Gr: PROC[a: Vec, b: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a.x>b.x AND a.y>b.y]
};
Eq: PROC[a: Vec, b: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a=b]
};
In: PROC[a: Vec, b: Vec, c: Vec] RETURNS[BOOLEAN] = INLINE {
RETURN[a.x IN[b.x..c.x] AND a.y IN[b.y..c.y]]
};
Min: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE {
RETURN[[MIN[a.x,b.x],MIN[a.y,b.y]]]
};
Max: PROC[a: Vec, b: Vec] RETURNS[Vec] = INLINE {
RETURN[[MAX[a.x,b.x],MAX[a.y,b.y]]]
};
Dot: PROC[a: Vec, b: Vec] RETURNS[REAL] = INLINE {
RETURN[a.x*b.x+a.y*b.y]
};
Cross: PROC[a: Vec, b: Vec] RETURNS[REAL] = INLINE {
RETURN[a.x*b.y-a.y*b.x]
};
Mag:PROCEDURE[v:Vector.Vec] RETURNS[REAL] = INLINE { 
RETURN[RealFns.SqRt[v.x*v.x+v.y*v.y]]
};
Unit:PROCEDURE[v:Vector.Vec] RETURNS [Vec] = INLINE { 
m:REAL;
IF (m←Mag[v]) = 0 THEN RETURN[[0,0]] ELSE RETURN[[v.x/m,v.y/m]]
};
Normal:PROCEDURE[v:Vector.Vec] RETURNS [Vec] = INLINE {
RETURN[[-v.y,v.x]]
};
Matrix operations
Det: PROC[m: Matrix] RETURNS[REAL] = INLINE {
RETURN[m.a11*m.a22-m.a12*m.a21]
};
}.
Michael Plass, August 4, 1982 11:58 am. Made Vec the same as GraphicsBasic.Vec