gpt4 book ai didi

c++ - 计算圆、矩形和三角形坐标内的点 C++

转载 作者:行者123 更新时间:2023-11-28 05:47:03 25 4
gpt4 key购买 nike

我正在尝试制作一个程序来计算圆、三角形和 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 vector 。我不确定您要将排序算法用于什么目的。

此外,无需重新发明轮子。如果一个点在多边形(或圆)内,则有现有的算法来检查。您需要做的就是针对所有点测试它并计算形状内的点数。圆是微不足道的:只需检查从你的点到中心的距离是否小于半径。多边形有点复杂:point in polygon . ray-casting, even-odd rule algorithm可以这样实现:

#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) 的圆

请注意,光线转换算法也适用于更多的角(对于更多的角,角的顺序定义多边形)。

关于c++ - 计算圆、矩形和三角形坐标内的点 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com