栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++几何运算

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++几何运算

凸多边形面积运算将其拆分,运用海伦公式进行求和。

#include
#include
#include
#include
#include
#include
#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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号