查找圆和矩形之间的交点的弧线 [英] Find the arc of the intersection between circle and rectangle

查看:303
本文介绍了查找圆和矩形之间的交点的弧线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个从圆和矩形的交点创建的最大弧.我有圆的中心,半径和矩形的坐标,我需要找到与圆的中心的交点的角度.

I need to find the largest arc created from the intersection of a circle and a rectangle. I have the center of the circle, the radius and the coordinates of the rectangle, and I need to find the angle of the intersection points with the center of the circle.

我有一个有效的代码,但它计算出圆周点的解,而我想知道是否有一种更优雅的方法来使用三角函数而不是蛮力"来计算解.

I have a code that works, but it computes the solution iterating the points of the circumference, and I was wondering if there's a more elegant way to calculate the solution using trigonometry instead of "brute force".

那是我的代码:

import 'dart:math';

class CircleTheSquare {
  final Point     _circleCenter;
  final int       _circleRadius;
  final Rectangle _box;


  CircleTheSquare(this._circleCenter, this._circleRadius, this._box);


  Map<String, double> get arc {
    Map res = new Map();

    double angle = .0;
    double angleIn;
    double angleOut;
    double increment = 1.0;

    while (true) {
      if (angle > 360.0 && angleIn == null) {
        break;
      }

      // Finds a point of intersection (next points will be inside 
      // of the rectangle).
      if (!_isOutside(angle) && _isOutside(angle - increment)) {
        angleIn = angle;
      }

      // Finds next intersection (next points will be outside 
      // of the rectangle).
      if (angleIn != null &&
          _isOutside(angle + increment) && !_isOutside(angle)) {

        angleOut = angle;

        // Adds the arc to result only there's not a previous largest arc.
        if (res["in"] == null ||
            angleOut - angleIn > res["arc"]) {

          res["in"]  = angleIn;
          res["arc"] = angleOut - angleIn;
        }
        angleIn = null;
        angleOut = null;
      }
      angle += increment;
    }

    // If there's no intersections. 
    // -- For simplicity, we will assume that the
    //    rectangle and the circle intersect or that the circle is 
    //    inside of the rectangle).
    if (res["in"] == null) {
      res = {"in" : 0.0, "arc" : 360.0};
    }

    return res;
  }


  bool _isOutside(double a) {
    var res;

    double cx = _circleCenter.x + (_circleRadius * cos(a * (PI / 180)));
    double cy = _circleCenter.y + (_circleRadius * sin(a * (PI / 180)));

    bool hOut = cx < _box.left || cx > _box.left + _box.width;
    bool vOut = cy < _box.top || cy > _box.top + _box.height;

    if (hOut || vOut) {
      res = true;
    } else {
      res = false;
    }

    return res;
  }
}


main() {
  CircleTheSquare a = new CircleTheSquare(new Point(250, 250), 100,
                                          new Rectangle(0,0,500,500));

  print(a.arc); // {in: 0.0, arc: 360.0}

  CircleTheSquare b = new CircleTheSquare(new Point(450, 250), 100,
                                          new Rectangle(0,0,500,500));

  print(b.arc); // {in: 60.0, arc: 240.0}

  CircleTheSquare c = new CircleTheSquare(new Point(420, 420), 100,
                                          new Rectangle(0,0,500,500));

  print(c.arc); // 4 intersections, returns the largest arc:
                          // {in: 127.0, arc: 196.0}
}

推荐答案

  1. 为简单起见,请移动所有坐标以使圆以零为中心(box.left = box.left-circlecenter.x等)
  2. 找到圆与矩形边的交点.例如,对于左侧求解(box.left)^ 2 + y ^ 2 =半径^ 2,请检查该点是否位于侧面,并将相交点添加到列表中
  3. 按角度对相交点进行排序(可能是通过边检查顺序自动提供),找到矩形内圆弧的最大角度间隔

这篇关于查找圆和矩形之间的交点的弧线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆