Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { createDefaultPreset } = require("ts-jest");

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import("jest").Config} **/
module.exports = {
preset: 'ts-jest',
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};
61 changes: 33 additions & 28 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import Point from "./point";

export default class Line {
point1: Point
point2: Point
n: number | undefined
slope: number | undefined
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }: { point1?: Point, point2?: Point, n?: number, slope?: number } = {}) {
import Point from './point';

class Line {
point1: Point;
point2: Point;
slope?: number;
n?: number;

constructor({ point1, point2 }: { point1: Point; point2: Point }) {
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}



calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
this.slope = (this.point2.y - this.point1.y) / (this.point2.x - this.point1.x);
}

calculateNOfLineFunction() {
if (this.slope)
this.n = this.point1.y - this.slope * this.point1.x
if (this.slope !== undefined) {
this.n = this.point1.y - this.slope * this.point1.x;
}
}

getPointOnXAsis() {
return this.getPointByY(0)
if (this.slope !== 0 && this.slope !== undefined && this.n !== undefined) {
const x = -this.n / this.slope;
return new Point({ x, y: 0 });
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is double code, you can call the function

return undefined;
}

getPointOnYAsis() {
return this.getPointByX(0)
getPointOnYAsis() {
if (this.n !== undefined) {
return new Point({ x: 0, y: this.n });
}
return undefined;
}


getPointByX(x: number) {
if (this.slope && this.n) {
let y = this.slope * x + this.n
return new Point({ x, y })
if (this.slope !== undefined && this.n !== undefined) {
const y = this.slope * x + this.n;
return new Point({ x, y });
}
return undefined;
}

getPointByY(y: number) {
if (this.slope && this.n) {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
if (this.slope !== 0 && this.slope !== undefined && this.n !== undefined) {
const x = (y - this.n) / this.slope;
return new Point({ x, y });
}
return undefined;
}


}

export default Line;
31 changes: 15 additions & 16 deletions modules/geometry-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@ import Point from './ecs6-class/point';

export const calculateDistance = (point1: Point, point2: Point): number => {
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Point | undefined => {
if (
line1.slope === undefined || line2.slope === undefined ||
line1.n === undefined || line2.n === undefined
) {
return undefined;
}
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
}
else {
return false
}
}
else {
if (line1.n !== undefined && line1.slope !== undefined && line2.n !== undefined && line2.slope !== undefined) {
const x = (line1.n - line2.n) / (line2.slope - line1.slope)
const junctionPoint = line1.getPointByX(x);
return junctionPoint
return true;
} else {
return false;
}
} else {
const x = (line1.n - line2.n) / (line2.slope - line1.slope);
const junctionPoint = line1.getPointByX(x);
return junctionPoint;
}
}

export const isPointOnLine = (line: Line, point: Point): Boolean => {
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
return true
}
proxyLine.calculateNOfLineFunction()
}
return false
}
Expand Down
189 changes: 189 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading