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
56 changes: 31 additions & 25 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
import Point from "./point";

export default class Line {
point1: Point
point2: Point
n: number | undefined
slope: number | undefined
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 } = {}) {
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.point1.y - this.point2.y) / (this.point1.x - this.point2.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;
}
}
ensureSlopeAndN() {
if (this.slope === undefined) {
this.calculateSlope();
}
if (this.n === undefined) {
this.calculateNOfLineFunction();
}
}

getPointOnXAsis() {
return this.getPointByY(0)
this.ensureSlopeAndN();
return this.getPointByY(0);
}

getPointOnYAsis() {
return this.getPointByX(0)
this.ensureSlopeAndN();
return this.getPointByX(0);
}


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

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


}
}
99 changes: 99 additions & 0 deletions modules/tests/line.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Line from "../ecs6-class/line";
import Point from "../ecs6-class/point";

describe("Line Class", () => {
test("Constructor initializes correctly", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 4 });
const line = new Line({ point1, point2 });
expect(line.point1).toEqual(point1);
expect(line.point2).toEqual(point2);
expect(line.slope).toBeUndefined();
expect(line.n).toBeUndefined();
});

test("calculateSlope calculates slope correctly", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
expect(line.slope).toBe(2);
});

test("calculateNOfLineFunction calculates n correctly", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

don't call two functions in one test
each test executes only one function
if you expect that a certain function will also be executed, do it inside the function

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

still more then one function is executed

expect(line.n).toBe(0);
});

test("ensureSlopeAndN ensures slope and n are calculated", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });

expect(line.slope).toBeUndefined();
expect(line.n).toBeUndefined();

line.ensureSlopeAndN();

expect(line.slope).toBe(2);
expect(line.n).toBe(0);
});

test("getPointOnXAsis returns correct point", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const pointOnXAxis = line.getPointOnXAsis();
expect(pointOnXAxis).toEqual(new Point({ x: 0, y: 0 }));
});

test("getPointOnYAsis returns correct point", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const pointOnYAxis = line.getPointOnYAsis();
expect(pointOnYAxis).toEqual(new Point({ x: 0, y: 0 }));
});

test("getPointByX calculates point correctly", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const point = line.getPointByX(2);
expect(point).toEqual(new Point({ x: 2, y: 4 }));
});

test("getPointByY calculates point correctly", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const point = line.getPointByY(4);
expect(point).toEqual(new Point({ x: 2, y: 4 }));
});

test("getPointByX handles undefined slope or n", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });

expect(line.slope).toBeUndefined();
expect(line.n).toBeUndefined();

line.getPointByX(2);

expect(line.slope).toBe(2);
expect(line.n).toBe(0);
});
});
67 changes: 67 additions & 0 deletions modules/tests/point.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Point from "../ecs6-class/point";
import Line from "../ecs6-class/line";
// filepath: d:\chany waingort\build-tests-ts\modules\ecs6-class\point.test.ts

describe("Point Class", () => {
test("Constructor initializes correctly", () => {
const point = new Point({ x: 5, y: 10 });
expect(point.x).toBe(5);
expect(point.y).toBe(10);
});

test("Constructor initializes with default values", () => {
const point = new Point();
expect(point.x).toBe(0);
expect(point.y).toBe(0);
});

test("moveVertical moves the point vertically", () => {
const point = new Point({ x: 5, y: 10 });
point.moveVertical(3);
expect(point.y).toBe(13); // 10 + 3
});

test("moveHorizontal moves the point horizontally", () => {
const point = new Point({ x: 5, y: 10 });
point.moveHorizontal(-2);
expect(point.x).toBe(3); // 5 - 2
});
});

test("Constructor initializes with default values", () => {
const line = new Line();
expect(line.point1).toEqual(new Point());
expect(line.point2).toEqual(new Point());
expect(line.slope).toBeUndefined();
expect(line.n).toBeUndefined();
});

test("Constructor initializes with custom values", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 4 });
const line = new Line({ point1, point2, slope: 2, n: 0 });
expect(line.point1).toEqual(point1);
expect(line.point2).toEqual(point2);
expect(line.slope).toBe(2);
expect(line.n).toBe(0);
});

test("getPointOnXAsis returns correct point", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const pointOnXAxis = line.getPointOnXAsis();
expect(pointOnXAxis).toEqual(new Point({ x: 0, y: 0 }));
});

test("getPointOnYAsis returns correct point", () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 6 });
const line = new Line({ point1, point2 });
line.calculateSlope();
line.calculateNOfLineFunction();
const pointOnYAxis = line.getPointOnYAsis();
expect(pointOnYAxis).toEqual(new Point({ x: 0, y: 0 }));
});
Loading