Published
Edited
Nov 20, 2021
Fork of Point
Importers
Insert cell
Insert cell
Insert cell
class Point {
constructor(x, y, color) {
this.x = x;
this.y = y;
}

equals(point) {
return this.x === point.x && this.y === point.y;
}

clone() {
return new Point(this.x, this.y);
}

isZero() {
return this.x === 0 && this.y === 0;
}

angle() {
//The return angle range is -180° to 180°
return Math.atan2(this.y, this.x) * (180 / Math.PI);
}

length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
distance(destination) {
const d = destination || new Point(0, 0);

const dx = this.x - d.x;
const dy = this.y - d.y;

return Math.sqrt(dx * dx + dy * dy);
}

add(point) {
return new Point(this.x + point.x, this.y + point.y);
}

substract(point) {
return new Point(this.x - point.x, this.y - point.y);
}

scale(scale, center) {
const c = center || new Point(0, 0);

const offSet = {x: this.x - c.x, y: this.y - c.y};

const targetX = offSet.x * scale + c.x;
const targetY = offSet.y * scale + c.y;
return new Point(targetX, targetY);
}

rotate(degree, center) {
const c = center || new Point(0, 0);

const theta = (Math.PI * degree) / 180.0;
const sin = Math.sin(theta);
const cos = Math.cos(theta);

const offSet = {x: this.x - c.x, y: this.y - c.y};

const targetX = offSet.x * cos - offSet.y * sin + c.x;
const targetY = offSet.x * sin + offSet.y * cos + c.y;
return new Point(targetX, targetY);
}

pointSymmetry(center) {
const c = center || new Point(0, 0);

const targetX = - (this.x - c.x) + c.x;
const targetY = - (this.y - c.y) + c.y;

return new Point(targetX, targetY);
}
}
Insert cell
Insert cell
{
let message = "";
const PA = new Point(1, 2);
// Equal
if (PA.equals(new Point(1, 2)) !== true) {
message += `Equals: PA.equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.equals(new Point(1, 1)) !== false) {
message += `Equals: PA.equals(new Point(1, 1)) !== false test failed. \n`;
}

// Clone
if (PA.equals(PA.clone()) !== true) {
message += `Clone: PA.equals(PA.clone()) !== true test failed. \n`;
}

// IsZero
if (PA.isZero() === true) {
message += `IsZero: PA.isZero() === true test failed. \n`;
}
if (new Point(0, 0).isZero() !== true) {
message += `IsZero: new Point(0, 0).isZero() !== true test failed. \n`;
}

// Angle
if (PA.angle() !== Math.atan2(2, 1) * (180 / Math.PI)) {
message += `Angle: PA.angle() !== Math.atan2(2, 1) * (180 / Math.PI) test failed. \n`;
}

if (new Point(1, 0).angle() !== 0) {
message += `Angle: new Point(1, 0).angle() !== 0 test failed. \n`;
}

if (new Point(0, 0).angle() !== 0) {
message += `Angle: new Point(0, 0).angle() !== 0 test failed. \n`;
}

if (new Point(1, 1).angle() !== 45) {
message += `Angle: new Point(1, 1).angle() !== 45 test failed. \n`;
}

if (new Point(0, 1).angle() !== 90) {
message += `Angle: Point(0, 1).angle() !== 90 test failed. \n`;
}
if (new Point(-1, 0).angle() !== 180) {
message += `Angle: Point(-1, 0).angle() !== 180 test failed. \n`;
}

if (new Point(-1, -1).angle() !== -135) {
message += `Angle: new Point(-1, -1).angle() !== -135 test failed. \n`;
}
// Length
if (PA.length() !== Math.sqrt(5)) {
message += `Length: PA.length() !== Math.sqrt(5) test failed. \n`;
}
if (new Point(0, 0).length() !== 0) {
message += `Length: new Point(0, 0).length() !== 0 test failed. \n`;
}

if (new Point(1, 0).length() !== 1) {
message += `Length: new Point(1, 0).length() !== 1 test failed. \n`;
}

if (new Point(0, 1).length() !== 1) {
message += `Length: new Point(0, 1).length() !== 1 test failed. \n`;
}

if (new Point(-1, -1).length() !== Math.sqrt(2)) {
message += `Length: new Point(-1, -1).length() !== Math.sqrt(2) test failed. \n`;
}

// Distance
if (PA.distance({ x: 1, y: 2 }) !== 0) {
message += `Distance: PA.distance({ x: 1, y: 2 }) !== 0 test failed. \n`;
}
if (PA.distance({ x: -1, y: -2 }) !== Math.sqrt(20)) {
message += `Distance: PA.distance({ x: -1, y: -2 }) !== Math.sqrt(20) test failed. \n`;
}

if (PA.distance({ x: 0, y: 0 }) !== Math.sqrt(5)) {
message += `Distance: PA.distance({ x: 0, y: 0 }) !== Math.sqrt(5) test failed. \n`;
}

// Add
if (PA.add(PA).equals(new Point(2, 4)) !== true) {
message += `Add: PA.add(PA).equals(new Point(2, 4)) !== true test failed. \n`;
}

if (PA.add(new Point(-1, -2)).isZero() !== true) {
message += `Add: PA.add(new Point(-1, -2)).isZero() !== true test failed. \n`;
}

if (PA.add(new Point(0, 0)).equals(PA) !== true) {
message += `Add: PA.add(new Point(0, 0)).equals(PA) !== true test failed. \n`;
}
// Substract
if (PA.substract(PA).isZero() !== true) {
message += `Substract: PA.substract(PA).isZero() !== true test failed. \n`;
}

if (PA.substract(new Point(-1, -2)).equals(new Point(2, 4)) !== true) {
message += `Substract: PA.substract(new Point(-1, -2)).equals(new Point(2, 4)) !== true test failed. \n`;
}

if (PA.substract(new Point(0, 0)).equals(PA) !== true) {
message += `Substract: PA.substract(new Point(0, 0)).equals(PA) !== true test failed. \n`;
}

// Scale
if (PA.scale(0.5).equals(new Point(0.5, 1)) !== true) {
message += `Scale: PA.scale(0.5).equals(new Point(0.5, 1)) !== true test failed. \n`;
}

if (PA.scale(1).equals(new Point(1, 2)) !== true) {
message += `Scale: PA.scale(1).equals(new Point(1, 2)) !== true test failed. \n`;
}
if (PA.scale(0.5, { x: 2, y: 3 }).equals(new Point(1.5, 2.5)) !== true) {
message += `Scale: PA.scale(0.5, { x: 2, y: 3 }).equals(new Point(1.5, 2.5)) !== true test failed. \n`;
}

if (PA.scale(0, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true) {
message += `Scale: PA.scale(0.5, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.scale(2,{ x: 1, y: 2 }).equals(new Point(1, 2)) !== true) {
message += `Scale: PA.scale(2, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true test failed. \n`;
}

// Rotate
if ((PA.rotate(90).x === -2 && Math.round(PA.rotate(90).y) === 1) !== true) {
message += `Rotate: (PA.rotate(90).x === -2 && Math.round(PA.rotate(90).y) === 1) !== true test failed. \n`;
}

if (PA.rotate(0).equals(new Point(1, 2)) !== true) {
message += `Rotate: PA.rotate(0).equals(new Point(1, 2)) !== true test failed. \n`;
}
if (PA.rotate(90, { x: 2, y: 3 }).equals(new Point(3, 2)) !== true) {
message += `Rotate: PA.rotate(90, { x: 2, y: 3 }).equals(new Point(3, 2)) !== true test failed. \n`;
}

if (PA.rotate(0, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true) {
message += `Rotate: PA.rotate(0, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.rotate(90, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true) {
message += `Rotate: PA.rotate(90, { x: 1, y: 2 }).equals(new Point(1, 2)) !== true test failed. \n`;
}

// PointSymmetry
if (PA.pointSymmetry({ x: 0, y: 0}).equals(new Point(-1, -2)) !== true) {
message += `pointSymmetry: PA.symmetry({ x: 0, y: 0}).equals(new Point(-1, -2)) !== true test failed. \n`;
}

if (PA.pointSymmetry({ x: 1, y: 2}).equals(new Point(1, 2)) !== true) {
message += `pointSymmetry: PA.symmetry({ x: 1, y: 2}).equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.pointSymmetry({ x: 0, y: 2}).equals(new Point(-1, 2)) !== true) {
message += `pointSymmetry: PA.symmetry({ x: 1, y: 2}).equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.pointSymmetry({ x: 1, y: 0}).equals(new Point(1, -2)) !== true) {
message += `pointSymmetry: PA.symmetry({ x: 1, y: 0}).equals(new Point(1, -2)) !== true test failed. \n`;
}

// Succeed?
if (message === "") {
message = "Succeed";
}

return md`${message}`;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more