获取弧/扇区的每个像素 [英] Get every pixel of an arc/sector

查看:110
本文介绍了获取弧/扇区的每个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模拟器中,我试图检查弧线的每个像素。中心(x& y),半径和弧的角度(半径,currentAngle和newAngle)具有固定的协调关系,所以我可以用颜色填充以表示它给用户。但是我需要对覆盖的像素执行一些操作。
我试过的是:

pre $ for(int i = 0; i 为(INT J = 0; J<半径; J ++){
指向点=新点((int)的(X + J * Math.cos(currentAngle中 - Math.toRadians(I))), (int)(y + j * Math.sin(currentAngle - Math.toRadians(i))));

check(point.x,point.y);

我认为可以改变角度和半径,但是,正如我后面所理解的,许多像素错过了,因为360度以外的每个度数都包含超过1个像素。



我尝试搜索网页并找到了一些方法通过圆的每个像素。没有设法把它改造成弧形像素。


解决方案

您可以从极坐标到属于笛卡尔和迭代点开关段:

  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;
双XMAX = curCos *半径
双R2 =半径*半径

。对于(INT I = 0; I< XMAX;我++){
为(INT j = curTan * x; j if(i * i + j * j> r2){
continue;
}
Point point = new Point(x + i,y + j);
}
}

此代码段仅覆盖情况下,当newAngle> currentAngle中和整个片段位于第一象限(x> 0且y> 0的区域),但是您可以了解如何迭代点以及如何概括任何角度组合的解。


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.

解决方案

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.

这篇关于获取弧/扇区的每个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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