dope2/lib/point.js

65 lines
2.3 KiB
JavaScript

import { Double } from "../primitives/symbols.js";
import { nominalType, NominalType } from "../structures/nominal_type.js";
import { makeProductType } from "../structures/product.js";
import { typedFnType } from "../metacircular.js";
import { prodType } from "../structures/product.js";
const PointCartesian2D = nominalType(
"PointCartesian2D")
// just a unique number, to make sure that our type is only equal to itself
// BigInt("0xBBAAD62B10EE21993BA690A732DA2A6875CE4B6F5E7139D5AEC9FD887F9D24A8"))
(prodType(Double, Double));
const PointPolar2D = nominalType(
"PointPolar2D")
// 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 = => ({left: r, right: θ}) => {
return {left: r, right: θ+};
}
export const scale = dr => ({left: r, right: θ}) => {
return {left: r+dr, right: θ};
}
const examplePoint = {left: 1, right: 2};
export const ModulePoint = {l:[
{i: -1 , t: Double},
{i: 2 , t: Double},
{i: examplePoint , t: PointCartesian2D},
{i: examplePoint , t: prodType(Double, Double)},
...makeProductType(Double, Double).l,
{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})})),
]};