简体   繁体   English

找到一个部门内部的要点

[英]Finding a point that is interior to a sector

SOLVED: See my answer . 求助:请参阅我的回答

I'm trying to find a point that is interior to an arc so when a floodfill occurs it does not accidentally fill an area outside the arc. 我试图找到弧内的点,以便在发生泛洪时不会意外地填充弧外的区域。 As long as the absolute value of the distance between the two angles: start and end; 只要两个角度之间的距离的绝对值是:开始和结束; are less than PI this works (There are a couple extreme edge cases where the drawn lines are so close that the point picked is part of those lines, but that's for a different day...). 小于PI的效果(在一些极端的情况下,绘制的线条非常接近,以至于选取的点是这些线条的一部分,但这是在另一天。)。

The problem I'm having is when the absolute value of the distance between the start and end angles is greater than PI the flood occurs on the exterior of the arc instead of the interior. 我遇到的问题是,当起始角度和终止角度之间的距离的绝对值大于PI时,泛洪发生在弧的外部而不是内部。 One example of many is: if the arc starts at 0 and ends at 3PI/2 the absolute value of the distance is 3PI/2, the flood occurs between the angles as if the absolute value distance were PI/2 and floods the entire screen except the pac-man-shaped arc. 许多示例之一是:如果圆弧从0开始并在3PI / 2处结束,则距离的绝对值为3PI / 2,则角度之间将发生泛洪,就像绝对值距离为PI / 2一样,并泛洪整个屏幕除了吃豆人形的弧形。

EDIT: 编辑:

To avoid confusion, here is the definition of an arc according to allegro (and trigonometry in general): 为避免混淆,这是根据快板(通常是三角函数)定义的弧:

void arc(BITMAP *bmp, int x, y, fixed ang1, ang2, int r, int color); 空弧(BITMAP * bmp,int x,y,固定ang1,ang2,int r,int color);

Draws a circular arc [minus the initial/terminal sides or center point] with centre [sic] x, y and radius r, in an anticlockwise [sic] direction starting from the angle a1 and ending when it reaches a2....Zero is to the right of the centre [sic] point, and larger values rotate anticlockwise [sic] from there. 画一个圆弧[减去起始/末端边或中心点],圆弧的中心[sic] x,y和半径为r,沿逆时针[sic]方向从角度a1开始,到到达a2时结束。...零位于中心[sic]点的右侧,并且较大的值从此处逆时针旋转[sic]。

Square brackets are my notation. 方括号是我的符号。

I've already taken care of converting from allegro's (stupid) use of fixed integers to the proper radian values. 我已经在照顾从快板(愚蠢的)使用fixed integers到适当的radian值的转换。

END EDIT 结束编辑

void Arc::Draw(BITMAP* dest, int color, bool filled, bool showCenter, bool showSides) {

    if(showSides || filled) {
        Line initial(GetX(), GetY(), GetZ(), GetStartPoint().GetX(), GetStartPoint().GetY(), GetZ(), false);
        initial.SetColor(color);

        Line terminal(GetX(), GetY(), GetZ(), GetEndPoint().GetX(), GetEndPoint().GetY(), GetZ(), false);
        terminal.SetColor(color);

        initial.Draw(dest, initial.GetColor(), false);
        terminal.Draw(dest, terminal.GetColor(), false);

    } else if(showCenter) {
        putpixel(dest, GetX(), GetY(), color);
    }

    //Draw arc first to prevent flood overflow.
    arc(dest, GetX(), GetY(), AngleConverter::RadianToFixed(_startAngle), AngleConverter::RadianToFixed(_endAngle), _radius, color);

    if(filled) {

        double distance = std::fabs(this->_endAngle - this->_startAngle);
        if(distance < a2de::A2DE_PI) {

            Line displace(GetStartPoint(), GetEndPoint(), false);
            Point displacePoint(displace.GetCenter());
            floodfill(dest, displacePoint.GetX(), displacePoint.GetY(), color);

        } else if(distance > a2de::A2DE_PI) {

            Line displace(GetStartPoint(), GetEndPoint(), false);
            Vector2D center_of_displacement(displace.GetCenter());
            Vector2D center_point(this->_center);
            Vector2D direction_of_center(center_of_displacement - center_point);

            double angle = std::atan2(direction_of_center.GetY(), direction_of_center.GetX());
            Vector2D flood_point = center_point - direction_of_center;
            flood_point += angle;

            double x = flood_point.GetX() > 0.0 ? std::ceilf(flood_point.GetX()) : std::floorf(flood_point.GetX());
            double y = flood_point.GetY() > 0.0 ? std::ceilf(flood_point.GetY()) : std::floorf(flood_point.GetY());
            floodfill(dest, x, y, color);

        } else {

            if(_startAngle == 0.0 || _endAngle == a2de::A2DE_2PI) {
                floodfill(dest, GetX(), GetY() - 1, color);
            } else if(_endAngle == 0.0 || _startAngle == a2de::A2DE_PI) {
                floodfill(dest, GetX(), GetY() + 1, color);
            }

        }

    }
}

First, about your 'sic' comments. 首先,关于您的“原文”评论。 The circle center point is usually called the center also of arcs, and counter clockwise is the most common convention. 圆心通常称为中心还的弧线和逆时针最常见的约定。 Just as the x axis points to the right and the y axis up, and angles start at the positive x axis. 正如x轴指向右侧,y轴指向上方,角度从x轴正方向开始。

To find out if a point x,y is within a region defined in polar coordinates (r,eta) you just convert the point x_point,y_point into polar coordinates 要确定点x,y是否在极坐标(r,eta)定义的区域内,只需将点x_point,y_point转换为极坐标

r_point=sqrt((x_point-x_circle)^2 + (y_point-y_circle)^2 )
eta_point=atan2((y_point-y_circle) , (y_point-x_circle))

Use atan2, then you need not think about signs and pi-flips etc. what is the difference between atan and atan2 in c++? 使用atan2,则无需考虑符号和pi-flips等。c ++中的atan和atan2有什么区别?

Now, is the radious within the 'sector' ?
if (r_point<r_sector) ... 

If that's the case it's worth taking a look at the angular part: Subtract the star angle from both eta_point and the angular size of the sector 如果是这种情况,那么值得看一下角度部分:从eta_point和扇形的角度大小中减去星形角

eta_point_new = eta_point - ang1
ang2_new = ang2 - ang1

Now, ang2_new is how big the sector is in the rotational direction, and eta_point_new is how far away the point is. 现在,ang2_new是该扇区在旋转方向上的大小,而eta_point_new是该点在多远的位置。 If ang2_new is negative, it means the sector crossed the border of your angular coordinate, so you need to add 2pi to it. 如果ang2_new为负,则意味着该扇区越过了角坐标的边界,因此需要向其添加2pi。 Then: 然后:

if (eta_point_new < ang2_new) 
   ... then the point is inside...

I'm sorry that I did not have time to test this or write it in proper C++, do with it what you like. 很抱歉,我没有时间测试或使用适当的C ++编写它,请随便使用它。

The positive rotation direction is counterclockwise. 正旋转方向为逆时针方向。 I am giving pseudocode, not a real C++ code. 我提供的是伪代码,而不是真正的C ++代码。

To determine the angle of rotation, subtract start angle from end angle. 要确定旋转角度,请从终止角度减去起始角度。 Do not take the absolute value. 不要取绝对值。 Normalize to the interval [0, 2π). 归一化为间隔[0,2π)。

rot_angle = end_angle - start_angle;

while (rot_angle < 0)   
  rot_angle += TWO_PI;

Now we need to take a point halfway between the center and the start point of the arc, and rotate it around the center by the one half of the total angle of rotation we have just found: 现在,我们需要在圆弧的中心和起点之间取一个点,然后将其围绕中心旋转刚刚找到的总旋转角度的一半:

start_point = rotate (point (center.x+r, center.y), center, start_angle);
interior_point = rotate (midpoint (center, start_point), center, rot_angle/2);

To rotate a point pt about an origin o by an angle of theta : 将点pt绕原点o旋转theta角:

point rotate (point pt, point o, double theta)
{
  return point(cos(theta) * (pt.x-o.x) - sin(theta) * (pt.y-o.y) + o.x,
               sin(theta) * (pt.x-o.x) + cos(theta) * (pt.y-o.y) + o.y);

}

For completeness: 为了完整性:

point midpoint (point p1, point p2)
{
   return point((p1.x+p2.x)/2, (p1.y+p2.y)/2);
}

SOLVED: 解决了:

1) Calculate Center of Arc: 1)计算弧中心:

double e = GetEndAngle();
double s = GetStartAngle();
double d = e - s;

double arc_center_x = 0.0;
double arc_center_y = 0.0;
double offset = 0.0;
if(d < 0.0) {
    offset = PI;
}
arc_center_x = (GetPosition().GetX() + std::cos(((s + e) / 2.0) + offset) * GetRadius());
arc_center_y = (GetPosition().GetY() + -std::sin(((s + e) / 2.0) + offset) * GetRadius());
_center = Vector2D(x, y);

2) Calculate Line from that center to the position of the sector: 2)计算从该中心到扇形位置的直线:

Line l(arc_center_x, arc_center_y, p_x, p_y);

3) Get the mid-point of that line which is always interior to the angles of the sector: 3)获得该线的中点,该点始终在扇形的角度内部:

double x = l.GetCenter().GetX();
double y = l.GetCenter().GetY();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM