繁体   English   中英

计算圆形,矩形和三角形的坐标内的点c ++

[英]Count For Points Within Coordinates for a circle, rectangle and triangle c++

我正在尝试制作一个程序来计算圆形,三角形和4边多边形内的点数。 我大约有3500个坐标,我希望能够找到这些形状内的坐标数的计数,例如,对于圆,我想放置中心坐标和半径并找到计数。 或者对于像正方形的形状,我目前有一个公式可以得到4个点,并使用冒泡排序,这样我就可以排序从这些点计算面积的方式,但是我想我会错了方式,我只是刚刚开始编写此代码,并将它提出来,但请稍作介绍,因为我只是想在填充它之前弄清楚它。

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

void bubblesort(vector<double>& vec) 
{
double s = vec.size();
for (int i=0; i<s; i++) 
    {
    for (int j=i+1; j<s; j++) 
        {
        if (vec[i] < vec[j]) 
            {
            double x = vec[i];
            vec[i] = vec[j];
            vec[j] = x;
            }
        }
    }
}

void squarecount(vector<double>& a, vector<double>& b)
{
    // I am thinking here to put the x,y coordinates, linking each two and getting 4 equations counting the number satisfying all 4 inequalities
}

int main ()
{
    double a;
    cout << "Please enter the corresponding numberthe shape for which you want to find the count of in your data;" << endl;
    cout << "Circle (1)" << endl;
    cout << "Triangle (2) " << endl;
    cout << "4 Sided Polygon (3)" << endl;
    cin >> a;

    if ( a == 1)
    {
        return 0;
    }
    else if (a == 2)
    {
        return 0;
    }
    else if (a == 3)
    {
        vector<double> data1;
        vector<double> data2;
        // 4 Sided Polygon
        double x1, x2, x3, x4, y1, y2, y3, y4;
        cout << "Enter the first x coordinate then press enter, then the corresponding y coordinate" << endl;
        cin >> data1[0];
        cin >> data2[0];
        cout << "Enter the second x coordinate then press enter, then the corresponding y coordinate" << endl;
        cin >> data1[1];
        cin >> data2[1];
        cout << "Enter the second x coordinate then press enter, then the corresponding y coordinate" << endl;
        cin >> data1[2];
        cin >> data2[2];
        cout << "Enter the second x coordinate then press enter, then the corresponding y coordinate" << endl;
        cin >> data1[3];
        cin >> data2[3];
        bubblesort(data1);
        bubblesort(data2);
    }
    else
    {
        cout << "Invalid Input!" << endl;
        cout << "Please enter either 1 for a circle, 2 for a triangle or 3 for a a 4 sided pollygon" << endl;

    }


 }

首先,如果要写入vector ,则应使用push_back(.)或首先resize向量的resize 我不确定您要使用排序算法的目的。

同样,也无需重新发明轮子。 现有的算法可以检查点是否在多边形(或圆形)内部。 您需要做的就是测试所有点,并计算形状内的点数。 圆是微不足道的:只需检查您的点到中心的距离是否小于半径。 多边形稍微复杂一点: 指向多边形 光线投射,奇偶规则算法可以这样实现:

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

bool checkPoly(double x, double y, vector<double> xCorners, vector<double> yCorners){

    int num=xCorners.size();
    int j=num-1;
    bool res=false;

    for(int i=0;i<num;i++){
        if((yCorners[i]>y) != (yCorners[j]>y) && (x<(xCorners[j]-xCorners[i])*(y-yCorners[i])/(yCorners[j]-yCorners[i])+xCorners[i])){
            res = !res;
        }
        j=i;
    }
    return res;
}

bool checkCircle(double x, double y, double xCenter, double yCenter, double radius){
    if(sqrt(pow(x-xCenter,2)+pow(y-yCenter,2)) < radius){
        return true;
    }
    return false;
}

int main(){
    //POLYGON
    vector<double> xCorners;
    vector<double> yCorners;
    xCorners.push_back(3);
    yCorners.push_back(2);

    xCorners.push_back(5);
    yCorners.push_back(6);

    xCorners.push_back(8);
    yCorners.push_back(4);
    bool resPoly=false;

    //CIRCLE
    double xCenter=5;
    double yCenter=4;
    double radius=2.5;
    bool resCircle=false;

    for(int x=0;x<10;x++){
        for(int y=0;y<10;y++){
            resPoly=checkPoly(x,y,xCorners,yCorners);
            resCircle=checkCircle(x,y,xCenter,yCenter,radius);
            if(resPoly){
                cout<<"("<<x<<", "<<y<<") is inside the polygon."<<endl;
            }else{
                cout<<"("<<x<<", "<<y<<") is outside the polygon."<<endl;
            }
            if(resCircle){
                cout<<"("<<x<<", "<<y<<") is inside the circle."<<endl;
            }else{
                cout<<"("<<x<<", "<<y<<") is outside the circle."<<endl;
            }
        }
    }
    return 0;
}

在此检查x = 0,...,10和y = 0,...,10的所有点是否都在内部

  1. 角为(3,2),(5,6)和(8,4)的三角形

  2. 半径为2.5且中心为(5,4)的圆

请注意,光线投射算法对于更多数量的角也是有效的(对于更多数量的角,角的顺序定义了多边形)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM