Get every pixel of an arc/sector

2019-09-16 13:58发布

In my simulator I am trying to check every pixel of an arc. There are fixed coordinated of the center ( x & y), radius and the angles of the arc (radius, currentAngle & newAngle), so I am able to fill it with color to represent it to user. But moreover I need to execute some actions over the covered pixels. What I have tried is:

for (int i = 0; i < newAngle; i++)
  for (int j = 0; j < radius; j++) {
    Point point = new Point((int)(x + j * Math.cos(currentAngle - Math.toRadians(i))), (int)( y + j * Math.sin(currentAngle - Math.toRadians(i))));

check(point.x, point.y);

I thought it was fine to go changing angles and radiuses, however, as I understood later, many pixels are missed due to the fact that on the border of an arc every degree out of 360 contains more than 1 pixel.

I tried searching the web and found some ways to go through every pixel of the circle. Didn't manage to transform it into the arc pixels.

标签: geometry
2条回答
Deceive 欺骗
2楼-- · 2019-09-16 14:36

You need to rasterize you arc - get all colored pixels in order. Famous Bresenham algorithm for circle is intended to do it. Just adapt it for drawing only needed part of the circle.
Yet another option - Midpoint circle algo

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-09-16 14:41

You can switch from polar coordinates to cartesian and iterate points belonging to segment:

double curCos = Math.cos(currentAngle);
double curSin = Math.sin(currentAngle);
double curTan = curSin/curCos;
double newCos = Math.cos(newAngle);
double newSin = Math.sin(newAngle);
double newTan = newSin/newCos;
double xMax = curCos*radius
double r2 = radius*radius

for(int i=0; i < xMax; i++) {
    for(int j=curTan*x; j < newTan*x; j++) {
        if(i*i + j*j > r2) {
            continue;
        }
        Point point = new Point(x + i, y + j);
    } 
}

This code snippet covers only case when newAngle>currentAngle and whole segment lies in first quadrant (area where x>0 and y>0), but you can get the idea how to iterate points and how to generalize the solution for any angles combination.

查看更多
登录 后发表回答