add nominal types for 2D points
This commit is contained in:
parent
94efde3e65
commit
18b5e56ff0
6 changed files with 110 additions and 21 deletions
57
lib/point.js
Normal file
57
lib/point.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { Function, Type } from "../metacircular.js";
|
||||
import { Double } from "../primitives/symbols.js";
|
||||
import { nominalType, NominalType } from "../structures/nominal_type.js";
|
||||
import { fnType, prodType, typedFnType } from "../type_registry.js";
|
||||
|
||||
const PointCartesian2D = nominalType(
|
||||
// just a unique number, to make sure that our type is only equal to itself
|
||||
BigInt("0xBBAAD62B10EE21993BA690A732DA2A6875CE4B6F5E7139D5AEC9FD887F9D24A8"))
|
||||
(prodType(Double, Double));
|
||||
|
||||
const PointPolar2D = nominalType(
|
||||
// just a unique number, to make sure that our type is only equal to itself
|
||||
BigInt("0x31CDAB4B3D84C4EB27D3C111FD7580E533268B72E05BD694F8B262913E018B72"))
|
||||
(prodType(Double, Double));
|
||||
|
||||
export const cart2polar = ({left: x, right: y}) => {
|
||||
const r = Math.sqrt(x*x + y*y);
|
||||
const θ = Math.atan(y/x);
|
||||
return {left: r, right: θ};
|
||||
};
|
||||
|
||||
export const polar2cart = ({left: r, right: θ}) => {
|
||||
const x = r * Math.cos(θ);
|
||||
const y = r * Math.sin(θ);
|
||||
return {left: x, right: y};
|
||||
}
|
||||
|
||||
export const translate = dx => dy => ({left: x, right: y}) => {
|
||||
return {left: x+dx, right: y+dy};
|
||||
}
|
||||
|
||||
export const rotate = dθ => ({left: r, right: θ}) => {
|
||||
return {left: r, right: θ+dθ};
|
||||
}
|
||||
|
||||
export const scale = dr => ({left: r, right: θ}) => {
|
||||
return {left: r+dr, right: θ};
|
||||
}
|
||||
|
||||
export const ModulePoint = {l:[
|
||||
{i: -1 , t: Double},
|
||||
{i: 2 , t: Double},
|
||||
{i: {left: 1, right: 2}, t: PointCartesian2D},
|
||||
|
||||
{i: PointCartesian2D , t: NominalType},
|
||||
{i: PointPolar2D , t: NominalType},
|
||||
|
||||
...typedFnType(cart2polar, fnType => fnType({in: PointCartesian2D, out: PointPolar2D})),
|
||||
|
||||
...typedFnType(polar2cart, fnType => fnType({in: PointPolar2D, out: PointCartesian2D})),
|
||||
|
||||
...typedFnType(translate , fnType => fnType({in: Double, out: fnType({in: Double, out: fnType({in: PointCartesian2D, out: PointCartesian2D})})})),
|
||||
|
||||
...typedFnType(rotate, fnType => fnType({in: Double, out: fnType({in: PointPolar2D, out: PointPolar2D})})),
|
||||
|
||||
...typedFnType(scale, fnType => fnType({in: Double, out: fnType({in: PointPolar2D, out: PointPolar2D})})),
|
||||
]};
|
||||
Loading…
Add table
Add a link
Reference in a new issue