在正方形和交叉周边内和周边找到(x,y)的算法

时间:2016-04-27 04:53:50

标签: algorithm shape point

我有一个学校作业,要求我找出某个点(x,y)是否在一个形状内,例如让我们说一个正方形。

 _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 500,  self.view.bounds.size.width, 50)];
[self.progressView setProgress:0.0];

[self.view addSubview:self.progressView];

 self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];

[![enter image description here][1]][1]//-----------------------------------------------------------------------

- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
    [self.timer invalidate];
    self.timer=nil;
  //  [self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
    self.usedTime += 1;
    CGFloat progress = self.usedTime*(0.0033333333);

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
    if(self.usedTime>200){
        [self.progressView setProgressTintColor:[UIColor redColor]];}
}
}


//-----------------------------------------------------------------------


- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressView setProgress:fprogress animated:YES];
}

//-----------------------------------------------------------------------

十字架有12分,

Class Square
private:
    int coordX[4];
    int coordY[4];

这些是广场的属性。那么让我们说方形坐标是(1,1)(1,4)(4,1)(4,4),我该如何找出点(2,2)是否在广场内?

Class Cross
private:
    int coordX[12];
    int coordY[12];

该函数用于获取两个整数,这是您要检查的点,它将返回true或false。

bool isPointInShape (int, int)

检查点是否在形状周边的函数也是如此。

有人可以帮助确定如何计算这两个函数的算法是什么?

1 个答案:

答案 0 :(得分:0)

class Square {
private:
    int coordX[4];
    int coordY[4];
    int xMin, xMax, yMin, yMax;

public:
    Square();
    bool is_point_on_shape (int, int);
};

Square::Square(int coordXIn[], int coordYIn[])
{
    coordX = coordXIn;
    coordY = coordYIn;

    // determine extreme x and y values, which define the box
    xMin = coordX[0];
    xMax = coordX[0];
    yMin = coordY[0];
    yMax = coordY[0];

    for (int i=1; i < 4; ++i)
    {
        if (coordX[i] < xMin)
        {
            xMin = coordX[i];
        }

        if (coordX[i] > xMax)
        {
            xMax = coordX[i];
        }
        if (coordY[i] < YMin)
        {
            yMin = coordY[i];
        }

        if (coordY[i] > yMax)
        {
            yMax = coordY[i];
        }
    }
}

// since we have the range of x and y values, checking to see if a point be
// inside the square just means checking that this point lies within the range
bool Square::is_point_on_shape (int x, int y)
{
    if (xMin + x > xMax) return false;
    if (yMin + y > yMax) return false;

    return true;
}