sunshineman1986

计算五星红旗上每颗小星各十个点纵横坐标的程序

写完了国际象棋棋牌和中国象棋棋牌,我又想写一个五星红旗的绘制程序。对于五星红旗,每个五角星都有十个点。大星还好办,小星的角度都是不规则的。怎么样做到“两点确定一个五角星”?于是我编写了这个程序。四个星直接有差别,不能硬搬。

//**************************************** 
//程序功能:输入五角星中心点和某一个角的顶点
//        坐标,输出其他各点坐标
//编制人:刘聪
//编制时间:2017年9月29日 
//****************************************

#include <iostream>
#include <cmath>
using namespace std;

int main(){
	float pi = 3.141592653;
	
	int center[2] = {300,60};
	int point_1[2] = {274,75};
	
	int point_2[2] = {0};
	int point_3[2] = {0};
	int point_4[2] = {0};
	int point_5[2] = {0};
	int point_6[2] = {0};
	int point_7[2] = {0};
	int point_8[2] = {0};
	int point_9[2] = {0};
	int point_10[2] = {0};
	
	point_2[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_2[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_3[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 2 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_3[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 2 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_4[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 3 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_4[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 3 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_5[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 4 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_5[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 4 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_6[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 5 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_6[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 5 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_7[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 6 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_7[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 6 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_8[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 7 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_8[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 7 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_9[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 8 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_9[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 8 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_10[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 9 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_10[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 9 + atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	cout << endl << "point_2[0] = " << point_2[0];
	cout << \'\t\' << "point_2[1] = " << point_2[1] << endl;
	
	cout << endl << "point_3[0] = " << point_3[0];
	cout << \'\t\' << "point_3[1] = " << point_3[1] << endl;
	
	cout << endl << "point_4[0] = " << point_4[0];
	cout << \'\t\' << "point_4[1] = " << point_4[1] << endl;
	
	cout << endl << "point_5[0] = " << point_5[0];
	cout << \'\t\' << "point_5[1] = " << point_5[1] << endl;
	
	cout << endl << "point_6[0] = " << point_6[0];
	cout << \'\t\' << "point_6[1] = " << point_6[1] << endl;
	
	cout << endl << "point_7[0] = " << point_7[0];
	cout << \'\t\' << "point_7[1] = " << point_7[1] << endl;
	
	cout << endl << "point_8[0] = " << point_8[0];
	cout << \'\t\' << "point_8[1] = " << point_8[1] << endl;
	
	cout << endl << "point_9[0] = " << point_9[0];
	cout << \'\t\' << "point_9[1] = " << point_9[1] << endl;
	
	cout << endl << "point_10[0] = " << point_10[0];
	cout << \'\t\' << "point_10[1] = " << point_10[1] << endl;
	
	return 0;
}



所求即最上面的那颗小星:

所求结果如下:(假定五星红旗宽为900,高为600)


第二个小星的center坐标为(360,120),point_1坐标为(330,124),代入程序计算得:


第三个小星的center坐标为(360,210),point_1坐标为(331,202),这时根据图样,两个角的加变成两个角的减,需要重写程序:

//**************************************** 
//程序功能:输入五角星中心点和某一个角的顶点
//        坐标,输出其他各点坐标
//编制人:刘聪
//编制时间:2017年9月29日 
//****************************************

#include <iostream>
#include <cmath>
using namespace std;

int main(){
	float pi = 3.141592653;
	
	int center[2] = {360,210};
	int point_1[2] = {331,202};
	
	int point_2[2] = {0};
	int point_3[2] = {0};
	int point_4[2] = {0};
	int point_5[2] = {0};
	int point_6[2] = {0};
	int point_7[2] = {0};
	int point_8[2] = {0};
	int point_9[2] = {0};
	int point_10[2] = {0};
	
	point_2[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_2[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_3[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 2 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_3[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 2 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_4[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 3 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_4[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 3 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_5[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 4 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_5[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 4 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_6[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 5 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_6[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 5 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_7[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 6 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_7[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 6 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_8[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 7 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_8[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 7 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_9[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 8 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_9[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 8 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_10[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 9 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_10[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 9 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	cout << endl << "point_2[0] = " << point_2[0];
	cout << \'\t\' << "point_2[1] = " << point_2[1] << endl;
	
	cout << endl << "point_3[0] = " << point_3[0];
	cout << \'\t\' << "point_3[1] = " << point_3[1] << endl;
	
	cout << endl << "point_4[0] = " << point_4[0];
	cout << \'\t\' << "point_4[1] = " << point_4[1] << endl;
	
	cout << endl << "point_5[0] = " << point_5[0];
	cout << \'\t\' << "point_5[1] = " << point_5[1] << endl;
	
	cout << endl << "point_6[0] = " << point_6[0];
	cout << \'\t\' << "point_6[1] = " << point_6[1] << endl;
	
	cout << endl << "point_7[0] = " << point_7[0];
	cout << \'\t\' << "point_7[1] = " << point_7[1] << endl;
	
	cout << endl << "point_8[0] = " << point_8[0];
	cout << \'\t\' << "point_8[1] = " << point_8[1] << endl;
	
	cout << endl << "point_9[0] = " << point_9[0];
	cout << \'\t\' << "point_9[1] = " << point_9[1] << endl;
	
	cout << endl << "point_10[0] = " << point_10[0];
	cout << \'\t\' << "point_10[1] = " << point_10[1] << endl;
	
	return 0;
}
计算结果为:



第四个小星的center坐标为(300,270),point_1坐标为(277,251),这时根据图样,point_2需要两个角相减,point_3需要36度减去之前point_2两个角的差,需要重写程序:

//**************************************** 
//程序功能:输入五角星中心点和某一个角的顶点
//        坐标,输出其他各点坐标
//编制人:刘聪
//编制时间:2017年9月29日 
//****************************************

#include <iostream>
#include <cmath>
using namespace std;

int main(){
	float pi = 3.141592653;
	
	int center[2] = {300,270};
	int point_1[2] = {277,251};
	
	int point_2[2] = {0};
	int point_3[2] = {0};
	int point_4[2] = {0};
	int point_5[2] = {0};
	int point_6[2] = {0};
	int point_7[2] = {0};
	int point_8[2] = {0};
	int point_9[2] = {0};
	int point_10[2] = {0};
	
	point_2[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos(atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])) - (float)36 / 360 * 2 * pi);
	point_2[1] = center[1] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin(atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])) - (float)36 / 360 * 2 * pi);
		
	point_3[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi - (atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])) - (float)36 / 360 * 2 * pi));
	point_3[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi - (atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])) - (float)36 / 360 * 2 * pi));
		
	point_4[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 3 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_4[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 3 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_5[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 4 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_5[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 4 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_6[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 5 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_6[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 5 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_7[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 6 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_7[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 6 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_8[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 7 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_8[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 7 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_9[0] = center[0] - (float)30 *
		cos((float)36 / 360 * 2 * pi * 8 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_9[1] = center[1] + (float)30 *
		sin((float)36 / 360 * 2 * pi * 8 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	point_10[0] = center[0] - (float)10 / cos((float)36 / 360 * 2 * pi) *
		cos((float)36 / 360 * 2 * pi * 9 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
	point_10[1] = center[1] + (float)10 / cos((float)36 / 360 * 2 * pi) *
		sin((float)36 / 360 * 2 * pi * 9 - atan((float)fabs(point_1[1] - center[1]) 
		/ (float)fabs(point_1[0] - center[0])));
		
	cout << endl << "point_2[0] = " << point_2[0];
	cout << \'\t\' << "point_2[1] = " << point_2[1] << endl;
	
	cout << endl << "point_3[0] = " << point_3[0];
	cout << \'\t\' << "point_3[1] = " << point_3[1] << endl;
	
	cout << endl << "point_4[0] = " << point_4[0];
	cout << \'\t\' << "point_4[1] = " << point_4[1] << endl;
	
	cout << endl << "point_5[0] = " << point_5[0];
	cout << \'\t\' << "point_5[1] = " << point_5[1] << endl;
	
	cout << endl << "point_6[0] = " << point_6[0];
	cout << \'\t\' << "point_6[1] = " << point_6[1] << endl;
	
	cout << endl << "point_7[0] = " << point_7[0];
	cout << \'\t\' << "point_7[1] = " << point_7[1] << endl;
	
	cout << endl << "point_8[0] = " << point_8[0];
	cout << \'\t\' << "point_8[1] = " << point_8[1] << endl;
	
	cout << endl << "point_9[0] = " << point_9[0];
	cout << \'\t\' << "point_9[1] = " << point_9[1] << endl;
	
	cout << endl << "point_10[0] = " << point_10[0];
	cout << \'\t\' << "point_10[1] = " << point_10[1] << endl;
	
	return 0;
}

计算结果为:


分类:

技术点:

相关文章: