0

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...).

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. 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.

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);

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.

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.

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);
            }

        }

    }
}
4

3 に答える 3

1

まず、あなたの「sic」コメントについて。円の中心点通常、円弧の中心とも呼ばれ、反時計回り最も一般的な規則です。x 軸が右を指し、y 軸が上を指し、角度が正の x 軸から始まるように。

ポイント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))

atan2 を使用すると、符号やパイフリップなどについて考える必要がなくなります。c++ での atan と atan2 の違いは何ですか?

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

その場合は、角度部分を見てみる価値があります: eta_point とセクターの角度サイズの両方から星の角度を引きます。

eta_point_new = eta_point - ang1
ang2_new = ang2 - ang1

ここで、ang2_new はセクターが回転方向にどれだけ大きいか、eta_point_new はポイントがどれだけ離れているかです。ang2_new が負の場合、セクターが角度座標の境界を越えたことを意味するため、2pi を追加する必要があります。それで:

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

申し訳ありませんが、これをテストしたり、適切な C++ で記述したりする時間がありませんでした。好きなようにしてください。

于 2012-07-10T18:36:56.867 に答える
0

解決済み:

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) その中心からセクターの位置までの線を計算します。

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

3) 常にセクターの角度の内側にあるその線の中点を取得します。

double x = l.GetCenter().GetX();
double y = l.GetCenter().GetY();
于 2012-07-29T20:29:08.543 に答える
0

正の回転方向は反時計回りです。実際の C++ コードではなく、疑似コードを提供しています。

回転角度を決定するには、終了角度から開始角度を引きます。絶対値を取らないでください。区間 [0, 2π) に正規化します。

rot_angle = end_angle - start_angle;

while (rot_angle < 0)   
  rot_angle += TWO_PI;

次に、円弧の中心と開始点の中間にある点を取得し、求めた合計回転角度の半分だけ中心を中心に回転させる必要があります。

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

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);

}

完全を期すために:

point midpoint (point p1, point p2)
{
   return point((p1.x+p2.x)/2, (p1.y+p2.y)/2);
}
于 2012-07-10T21:00:25.197 に答える