fork download
  1. class Rectangle {
  2. constructor(a, b) {
  3. this.x1 = Math.min(a.x, b.x);
  4. this.x2 = Math.max(a.x, b.x);
  5. this.y1 = Math.min(a.y, b.y);
  6. this.y2 = Math.max(a.y, b.y);
  7. }
  8.  
  9. isSquare() {
  10. return this.x2 - this.x1 === this.y2 - this.y1
  11. }
  12.  
  13. contains(dot) {
  14. return this.x2 > dot.x && this.x1 < dot.x && this.y2 > dot.y && this.y1 < dot.y
  15. }
  16.  
  17. get area() {
  18. return (this.x2 - this.x1) * (this.y2 - this.y1)
  19. }
  20.  
  21. intersect(rectangle) {
  22. if (this.x2 < rectangle.x1 || this.x1 > rectangle.x2 || this.y1 > rectangle.y2 || this.y2 < rectangle.y1) {
  23. return undefined
  24. } else {
  25. return new Rectangle({
  26. Math.max(this.x1, rectangle.x1), Math.max(this.y1, rectangle.y1)
  27. },
  28. {
  29. Math.min(this.x2, rectangle.x2), Math.min(this.y2, rectangle.y2)
  30. }
  31. )
  32. ;
  33.  
  34. // return {
  35. // x1: Math.max(this.x1, rectangle.x1),
  36. // x2: Math.min(this.x2, rectangle.x2),
  37. // y1: Math.max(this.y1, rectangle.y1),
  38. // y2: Math.min(this.y2, rectangle.y2)
  39. // }
  40. }
  41. }
  42. }
Runtime error #stdin #stdout #stderr 0s 107072KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.js:1:0 SyntaxError: class is a reserved identifier:
prog.js:1:0 class Rectangle {
prog.js:1:0 ^