Point.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. export class Point {
  2. constructor(x, y){
  3. this.x = x;
  4. this.y = y;
  5. }
  6. clone(point) {
  7. this.x = point.x
  8. this.y = point.y
  9. return this
  10. }
  11. scale(x, y) {
  12. this.x *= x;
  13. this.y *= y;
  14. return this
  15. }
  16. addition(point) {
  17. return new Point(this.x + point.x, this.y + point.y)
  18. }
  19. difference(point){
  20. return new Point(this.x - point.x, this.y - point.y)
  21. }
  22. equals(point) {
  23. return this.x == point.x && this.y == point.y
  24. }
  25. offset(point) {
  26. this.x += point.x
  27. this.y += point.y
  28. return this
  29. }
  30. angleTo(point) {
  31. return Math.atan2(point.y - this.y, point.x - this.x)
  32. }
  33. vectorTo(point) {
  34. let angle = this.angleTo(point)
  35. return new Point(Math.cos(angle), Math.sin(angle))
  36. }
  37. distanceTo(point) {
  38. return Math.hypot(point.y - this.y, point.x - this.x)
  39. }
  40. roundTo(point, threshholdX, threshholdY) {
  41. let diff = this.difference(point)
  42. if(Math.abs(diff.x) <= threshholdX) {
  43. this.x = point.x
  44. }
  45. if(Math.abs(diff.y) <= threshholdY) {
  46. this.y = point.y
  47. }
  48. }
  49. }