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
51 changes: 46 additions & 5 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
const Point = require("./point");

const Point = require("./point");
class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {

if (!(point1 instanceof Point)) {
throw new Error('point1 must be Point objects');
}
if (!(point2 instanceof Point)) {
throw new Error('point2 must be Point objects');
}
if (typeof n !== 'number' && n !== undefined) {
throw new Error('n must be a number');
}

if (typeof slope !== 'number' && slope !== undefined) {
throw new Error('slope must be a number');
}

this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
if (this.point1.x === this.point2.x) {
throw new Error("cut with zero");
}
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

Expand All @@ -26,14 +43,38 @@ class Line {


getPointByX(x) {
let y = this.slope * x + this.n
return new Point({ x, y })
if (x === undefined) {
throw new Error('x is undefined');
}
if (typeof x !== 'number') {
throw new Error('x isnt number');
}
if (!this.slope) {
this.calculateSlope();
}
if (!this.n) {
this.calculateNOfLineFunction();
}
let y = this.slope * x + this.n;
return new Point({ x, y });
}

getPointByY(y) {
if (y === undefined) {
throw new Error('y is undefined');
}
if (typeof y !== 'number') {
throw new Error('y isnt number');
}
if (!this.slope) {
this.calculateSlope();
}
if (!this.n) {
this.calculateNOfLineFunction();
}
let x = (y - this.slope) / this.n;
return new Point({ x, y })
}
}

module.exports = Line
module.exports = Line;
26 changes: 24 additions & 2 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
class Point {
constructor({x=0, y=0}={}) {
constructor({ x = 0, y = 0 } = {}) {
if (typeof (x) !== "number" && typeof (y) != "number") {
throw Error('x and y isnt number')
}
if (typeof (x) != "number") {
throw Error('x isnt number')
}
if (typeof (y) != "number") {
throw Error('y isnt number')

}
this.x = x;
this.y = y;
}

moveVertical(value) {
if (!value) {
throw Error('value is undefined')
}
if (typeof (value) !== "number") {
throw Error('value isnt number')
}
this.y += value;
}
moveHorizontal(value) {
if (!value) {
throw Error('value is undefined')
}
if (typeof (value) !== "number") {
throw Error('value isnt number')
}
this.x += value;
}
}

module.exports = Point
59 changes: 53 additions & 6 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line')
const Point = require("../modules/ecs6-class/point");

const calculateDistance = (point1, point2) => {
if (!(point1 instanceof Point) && !(point2 instanceof Point)) {
throw new Error('point1 and point2 must be objects');
}
if (!(point1 instanceof Point)) {
throw new Error('point1 must be objects');
}
if (!(point2 instanceof Point)) {
throw new Error('point2 must be objects');
}
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;
}

const calculateJunctionPoint = (line1, line2) => {
if (!(line1 instanceof Line) && !(line2 instanceof Line)) {
throw new Error('line1 and line2 must be Line');
}
if (!(line1 instanceof Line)) {
throw new Error('line1 must be Line');
}
if (!(line2 instanceof Line)) {
throw new Error('line2 must be Line');
}
if (!line1.slope) {
line1.calculateSlope();
}
if (!line1.n) {
line1.calculateNOfLineFunction();
}
if (!line2.slope) {
line2.calculateSlope();
}
if (!line2.n) {
line2.calculateNOfLineFunction();
}
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -22,15 +52,32 @@ const calculateJunctionPoint = (line1, line2) => {
return junctionPoint
}
}

const isPointOnLine = (line, point) => {
const proxyLine = new Line(line.point1, point)
if (!(line instanceof Line) && !(point instanceof Point)) {
throw new Error('line must be Line and point must be Point ');
}
if (!(point instanceof Point)) {
throw new Error('point must be Point');
}
if (!(line instanceof Line)) {
throw new Error('line must be Line');
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (!line.slope) {
line.calculateSlope();
}
if (!line.n) {
line.calculateNOfLineFunction();
}
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n2) {
if (line.n === proxyLine.n) {
return true
}
else {
return false
}
}
return false
}
Expand Down
45 changes: 45 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"test:coverage": "npm run test -- --coverage"
},
"dependencies": {
"devDependencies": {
"jest": "^29.7.0"
},
"repository": {
Expand Down
Loading