JavaScript 实现几何基础图形

JavaScript 实现几何基础图形

以下是常见几何图形的 JavaScript 实现,包含点、线段、圆、矩形和三角形的基本计算功能:

// 点类
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  
  // 计算两点距离
  distanceTo(point) {
    const dx = this.x - point.x;
    const dy = this.y - point.y;
    return Math.sqrt(dx * dx + dy * dy);
  }
  
  // 移动点
  translate(dx, dy) {
    return new Point(this.x + dx, this.y + dy);
  }
}

// 线段类
class LineSegment {
  constructor(start, end) {
    this.start = start;
    this.end = end;
  }
  
  // 计算长度
  length() {
    return this.start.distanceTo(this.end);
  }
  
  // 计算中点
  midpoint() {
    return new Point(
      (this.start.x + this.end.x) / 2,
      (this.start.y + this.end.y) / 2
    );
  }
}

// 圆类
class Circle {
  constructor(center, radius) {
    this.center = center;
    this.radius = radius;
  }
  
  // 计算周长
  circumference() {
    return 2 * Math.PI * this.radius;
  }
  
  // 计算面积
  area() {
    return Math.PI * this.radius * this.radius;
  }
  
  // 判断点是否在圆内
  containsPoint(point) {
    return this.center.distanceTo(point) <= this.radius;
  }
}

// 矩形类
class Rectangle {
  constructor(topLeft, width, height) {
    this.topLeft = topLeft;
    this.width = width;
    this.height = height;
  }
  
  // 计算面积
  area() {
    return this.width * this.height;
  }
  
  // 计算周长
  perimeter() {
    return 2 * (this.width + this.height);
  }
  
  // 获取中心点
  center() {
    return new Point(
      this.topLeft.x + this.width / 2,
      this.topLeft.y + this.height / 2
    );
  }
}

// 三角形类
class Triangle {
  constructor(pointA, pointB, pointC) {
    this.points = [pointA, pointB, pointC];
  }
  
  // 计算周长
  perimeter() {
    return (
      this.points[0].distanceTo(this.points[1]) +
      this.points[1].distanceTo(this.points[2]) +
      this.points[2].distanceTo(this.points[0])
    );
  }
  
  // 计算面积(海伦公式)
  area() {
    const a = this.points[0].distanceTo(this.points[1]);
    const b = this.points[1].distanceTo(this.points[2]);
    const c = this.points[2].distanceTo(this.points[0]);
    const s = (a + b + c) / 2;
    return Math.sqrt(s * (s - a) * (s - b) * (s - c));
  }
}

使用示例

// 创建点
const p1 = new Point(0, 0);
const p2 = new Point(3, 4);

// 计算距离
console.log("两点距离:", p1.distanceTo(p2)); // 输出: 5

// 创建线段
const line = new LineSegment(p1, p2);
console.log("线段长度:", line.length()); // 输出: 5
console.log("中点坐标:", line.midpoint()); // 输出: {x:1.5, y:2}

// 创建圆
const circle = new Circle(new Point(0, 0), 5);
console.log("圆周长:", circle.circumference().toFixed(2)); // 输出: 31.42
console.log("点是否在圆内:", circle.containsPoint(new Point(3, 4))); // 输出: true

// 创建矩形
const rect = new Rectangle(new Point(0, 0), 4, 3);
console.log("矩形面积:", rect.area()); // 输出: 12

// 创建三角形
const triangle = new Triangle(
  new Point(0, 0),
  new Point(4, 0),
  new Point(0, 3)
);
console.log("三角形面积:", triangle.area()); // 输出: 6

功能说明

  1. 点(Point):基础坐标表示,支持距离计算和平移
  2. 线段(LineSegment):由起点和终点构成,支持长度和中点计算
  3. 圆(Circle):由圆心和半径定义,支持周长、面积计算和点包含检测
  4. 矩形(Rectangle):由左上角顶点、宽度和高度定义,支持面积和周长计算
  5. 三角形(Triangle):由三个顶点定义,使用海伦公式计算面积

所有类都遵循面向对象设计原则,支持链式操作和扩展。可根据需要添加更多几何计算功能,如碰撞检测、旋转、缩放等。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值