计算每一个笛卡尔点转了一圈 [英] Calculating every cartesian point in a circle

查看:163
本文介绍了计算每一个笛卡尔点转了一圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:INT [] []草坪=新INT [980] [1280]

至极存储在草坪叶片的高度的值。

在我的模拟,我有一个机器人,绕着草坪和切割刀片。

我的机器人具有一个直径(rDiameter)的圆的形式。 该坐标系进行双和我的草坪是整数。

我已经developped二法则,即允许机器人切割草坪,但我并不满足,因为我的算法precisions不够高,性能也。

我的问题是,有没有办法做到这一点以外,我已经想到的想法?

或者我需要改变我的草坪的实施,以取得更好的成绩?

随意问什么,如果我是不太清楚。

下面是$ C $下我的两个算法(K代表机器人),centerPosition返回我的机器人中心(所以圆心)

 同方的做法
    INT bottomLeftCornerX =(int)的(k.getCenterPosition()的getX() -  simulParams.getKDiameter()/ 2);
    INT bottomLeftCornerY =(int)的(。k.getCenterPosition()的getY() -  simulParams.getKDiameter()/ 2);

    的for(int i = bottomLeftCornerX; I< bottomLeftCornerX + simulParams.getKDiameter();我++){
        对于(INT J = bottomLeftCornerY; J< bottomLeftCornerY + simulParams.getKDiameter(); J ++){
            ((LawnArea)lawn.getBladeHeight())cutBladeInArea(J,I);
        }
    }
 

在这里,一个与圈的做法(基本上这是circonference一级方程式present维基百科...):

 的(INT R = 0; R< simulParams.getKDiameter()/ 2; R ++){

        对于(双T = 0; T&2 * Math.PI; T = T + 0.1){
            的Point2D P =周长(k.getCenterPosition()的getX(),k.getCenterPosition()getY()以,T,R。);
            INT INTX =(int)的Math.ceil(p.getX());
            INT intY的=(int)的Math.ceil(p.getY());
                ((LawnArea)lawn.getBladeHeight())cutBladeInArea(intY的,INTX)。
            }
        }
    }
 

解决方案

有关可读性: k.x 的X坐标, k.y 是Y坐标, k.r 为半径。

 的for(int y =(int)的Math.max(0,Math.ceil(肯塔基州 -  KR)); Y< =(INT)Math.min(980,​​数学.floor(KY + KR)); Y ++){
    doulbe DX =的Math.sqrt(KR KR *  -  Math.abs(yk.y)* Math.abs(yk.y));
    对于(INT X = Math.max(0,(INT)Math.ceil(KX  -  DX)),X< =(INT)Math.min(1280,Math.floor(KX + DX)); X ++){
        切(X,Y);
    }
}  

有两个循环:
第一个是y坐标,并开始在 KY - KR 键,进入到 KY + KR 如果圈子是完全地在球场上(我不知道这是否永远是,如果是的话,那么你无法到达的角落)从顶部,因此您的屏幕的底部。
第二个是对于x坐标。该smalles和最大X进行计算的公式推(KX - X)的帮助下²+(KY - Y)2 =k.r²
随着你只能得到点在你的圈子。这一点,因为双到 INT 的转换。

I have an array: int[][] lawn = new int[980][1280];

wich stores the values of the height of the blades in the lawn.

In my simulation i have a robot that goes around the lawn and cuts the blades.

my robot has the form of a circle with a diameter (rDiameter). The coordinate system is done with Double and my lawn is in Integer.

I have developped two "algorithms" that allows the robot to cut the lawn but i'm not satisfied because the precisions of my algorithms is not high enough and the performance also.

My question is, is there a way to do this other than the ideas that I have already thought of?

Or do I need to change the implementation of my lawn to get better results?

Feel free to ask anything if i was not clear enough.

Here is the code for my two algorithms (k stands for the robot), centerPosition returns the center of my robot(so the center of the circle)

approach with square
    int bottomLeftCornerX = (int) (k.getCenterPosition().getX() - simulParams.getKDiameter() / 2);
    int bottomLeftCornerY = (int) (k.getCenterPosition().getY() - simulParams.getKDiameter() / 2);

    for (int i = bottomLeftCornerX; i < bottomLeftCornerX + simulParams.getKDiameter(); i++) {
        for (int j = bottomLeftCornerY; j < bottomLeftCornerY + simulParams.getKDiameter(); j++) {
            ((LawnArea) lawn.getBladeHeight()).cutBladeInArea(j, i);
        }
    }

And here the one with the circle approach (basically it's the circonference formule present in wikipedia...):

for (int r = 0; r < simulParams.getKDiameter() / 2; r++) {

        for (double t = 0; t < 2 * Math.PI; t = t + 0.1) {
            Point2D p = circumference(k.getCenterPosition().getX(), k.getCenterPosition().getY(), t, r);
            int intX = (int) Math.ceil(p.getX());
            int intY = (int) Math.ceil(p.getY());
                ((LawnArea) lawn.getBladeHeight()).cutBladeInArea(intY, intX);
            }
        }
    }

解决方案

For readability: k.x is the x coordinate, k.y is the y coordinate, k.r is the radius.

for(int y = (int)Math.max(0, Math.ceil(k.y - k.r)); y <= (int)Math.min(980, Math.floor(k.y + k.r)); y++) {
    doulbe dx = Math.sqrt(k.r * k.r - Math.abs(y-k.y) * Math.abs(y-k.y));
    for(int x = Math.max(0, (int)Math.ceil(k.x - dx)); x <= (int)Math.min(1280, Math.floor(k.x + dx)); x++) {
        cut(x,y);
    }
}

There are two loops:
The first one is for the y-coordinate and starts at k.y - k.r and goes until k.y + k.r if the circle is completly on the field (I didn't know if it always was, if yes then you can't reach the corners), so from the top to the bottom of your screen.
The second one is for the x coordinate. The smalles and biggest x are calculated with the help of the formular (k.x - x)² + (k.y - y)² = k.r².
With that you only get points in your circle. It's a little smaller because of double to int conversion.

这篇关于计算每一个笛卡尔点转了一圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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