凸多边形面积运算将其拆分,运用海伦公式进行求和。
#include <iostream>
#include <conio.h>
#include <iomanip>
#include<cstdlib>
#include <stdio.h>
#include <fstream>
#include "fun3.h"
using namespace std;
//求点之间的距离总和
void len(float x[80], float y[80],int v) {
float sum = 0;
for (int i = 0; i < v; i++) {
float k = x[i+1]-x[i];
float c = y[i + 1] - y[i];
float w = k * k + c * c;//两点距离平方
sum += sqrt(w);//累和
}
cout << sum;
}
//运用海伦公式,将多边形拆分成多个三角形进行求和
void area(float x[80], float y[80], int v) {
float sum=0;
for (int i = 1; i < v-1; i++) {
float d = x[i] - x[0];
float e = y[i] - y[0];
float f = x[i + 1] - x[0];
float g = y[i + 1] - y[0];
float h = x[i + 1] - x[i];
float j = y[i + 1] - y[i];//以第一个点为公共点,求三角形三个边距离
float a = sqrt(d * d + e * e);
float b = sqrt(f * f + g * g);
float c = sqrt(h * h + j * j);
float p = (a + b + c) / 2;
sum += sqrt(p*(p-a) * (p - b) * (p - c));
}
cout << sum;
}
void moveon(float x[80], float y[80], int v) {
float a;
float z[80];
float u[80];
cin >> a;
for (int i = 0; i < v; i++) {
z[i] = a;//对z数组进行初始化
}
ofstream ofile;
ofile.open("d:\\myfile2.txt");
for (int i = 0; i < v; i++) {
u[i] = y[i] + z[i];//对平移值进行更改
cout << setw(5) << x[i] << " " << setw(10) << u[i] << endl;
ofile << setw(5) << x[i] << " " << setw(10) << u[i] << endl;
}
ofile.close();
}
void moveunder(float x[80], float y[80], int v) {
float a;
float z[80];
float u[80];
cin >> a;
for (int i = 0; i < v; i++) {
z[i] = a;
}
ofstream ofile;
ofile.open("d:\\myfile2.txt");
for (int i = 0; i < v; i++) {
u[i] = y[i] - z[i];
cout << setw(5) << x[i] << " " << setw(10) << u[i] << endl;
ofile << setw(5) << x[i] << " " << setw(10) << u[i] << endl;
}
ofile.close();
}
void moveleft(float x[80], float y[80], int v) {
float a;
float z[80];
float u[80];
cin >> a;
for (int i = 0; i < v; i++) {
z[i] = a;
}
ofstream ofile;
ofile.open("d:\\myfile2.txt");
for (int i = 0; i < v; i++) {
u[i] = x[i] - z[i];
cout << setw(5) << u[i] << " " << setw(10) << y[i] << endl;
ofile << setw(5) << u[i] << " " << setw(10) << y[i] << endl;
}
ofile.close();
}
void moveright(float x[80], float y[80], int v) {
float a;
float z[80];
float u[80];
cin >> a;
for (int i = 0; i < v; i++) {
z[i] = a;
}
ofstream ofile;
ofile.open("d:\\myfile2.txt");
for (int i = 0; i < v; i++) {
u[i] = x[i] + z[i];
cout << setw(5) << u[i] << " " << setw(10) << y[i] << endl;
ofile << setw(5) << u[i] << " " << setw(10) << y[i] << endl;
}
ofile.close();
}
int main() {
int v;
int ch;
cout << "声明:若输入点个数为n,要输入n+1个点且最后一个点与第一个点相同" << endl;
cout << "请输入点的个数";
cin >> v;
float x[80];
float y[80];
for (int i = 0; i < v+1; i++) {
cin >> x[i] >> y[i];
}
cout << "图形周长为:";
len(x, y,v); //调用求图形长度的函数
cout << endl;
cout << "图形面积为:";//调用求图形面积函数
area(x, y,v);
cout << endl;
cout << "使用简介:w a s d控制上下左右";
cout << "输入平移方向和大小";
while (1) //获取键值,以wasd 调用进行不同平移方向的函数
{
if (_kbhit())
{
ch = _getch();
if (ch == 119) {
cout << "向上移动";
moveon(x,y,v);
}
if (ch == 115) {
cout << "向下移动";
moveunder(x,y,v);
}
if (ch == 97) {
cout << "向左移动";
moveleft(x, y,v);
}
if (ch == 100) {
cout << "向右移动";
moveright(x, y,v);
}
}
}
return 0;
}