多边形周长均分

题目要求:多边形由顶点坐标数组表示(vector<point> ver),将多边形周长均分成N等份,得到等分坐标数组(vector<point> ver_splist),其中N等份的第一个坐标点为ver[0]。

代码实现:

#include<stdafx.h>
#include <math.h>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct point{
	double x;
	double y;
};
//求两点距离
double dist(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//根据离b点距离off_len和a.b之间距离求坐标
point getpoint(point a,point b,double off_len,double len)
{
	point t_point;
	t_point.x=off_len/len*(a.x-b.x)+b.x;
	t_point.y=off_len/len*(a.y-b.y)+b.y;
	return t_point;
}

void split_avg(const vector <point> &src,int N,vector <point>  &split_dest)
{
	size_t point_num = src.size();
	double *row=new double[point_num];//边长
	double L=0;//周长
	//求边长
	size_t i=0;
	for(;i<point_num-1;i++)
	{
		row[i]=dist(src[i],src[i+1]);
		L+=row[i];
	    cout<<row[i]<<endl;
	}
        row[i]=dist(src[i],src[0]);
	L+=row[i];
	cout<<row[i];
        double avg=L/N;
        //求均分点坐标
	double sum_temp=row[0];
	double temp=0;
	i=0;
	for(int j=0;j<N;j++)
	{
		while(i<point_num)
		{
			if(avg>sum_temp)
			{				
				i++;
				sum_temp+=row[i];
			}
			else
			{
				temp=sum_temp-avg;
				if(i==(point_num-1))
					split_dest.push_back(getpoint(src[i],src[0],temp,row[i]));
				else
				    split_dest.push_back(getpoint(src[i],src[i+1],temp,row[i]));
				sum_temp=temp;
		    }
	    }
	}
}

void main()
{
	vector <point> src;
	vector <point> split_dest;
	int n;
	point tmp;
	cout<<"输入多边形坐标个数"<<endl;
	cin >> n;
	//多边形初始化
	for (int i = 0; i != n; i++) {
		cout<<"输出第"<<i<<"个顶点x坐标"<<endl;
		cin >> tmp.x;
		cout<<"输出第"<<i<<"个顶点y坐标"<<endl;
		cin >> tmp.y;
		src.push_back(tmp);
	}
	int N;
	do{
		cout<<"将多边形周长均分为几等份"<<endl;
		cin>>N;
	}while(N<=1);

        split_avg(src,N,split_dest);

	for (vector <point>::iterator it = split_dest.begin(); it != split_dest.end(); it++)
	{
		 cout << endl;
		 cout << (*it).x << ","<<(*it).y;     
	}
	getchar();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值