• 与三角形相关的问题 WITH 有向面积


    一、 输入三点的坐标,输出该三角形的面积

    海伦公式————

    //已知三点坐标求三角形面积 
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    #define MAX 100
    int main()
    {
        int x1,y1,x2,y2,x3,y3,i=0;
        double a,b,c,s,p,ss[MAX];
        cin>>x1>>y1>>x2>>y2>>x3>>y3;
        while(x1 || y1 || x2 || y2 || x3 || y3)
        {
            a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
            c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
            p=(a+b+c)*0.5;
            s=sqrt(p*(p-a)*(p-b)*(p-c)); 
            ss[i]=s;
            i++;
            cin>>x1>>y1>>x2>>y2>>x3>>y3;
        } 
        int n=i;
        for(int i=0; i<n; i++)
            printf("%.1f
    ",ss[i]);
        return 0;
    }

    海伦公式并不太好

    更好的方法 —— 有向面积

    #include<iostream>
    #include<cstdio>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
        double x0, x1, x2, y0, y1, y2, s;
        cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;
        while(x0 || y0 || x1 || y1 || x2 || y2)
        {
            s = (x0 * y1 + x1 * y2 + x2 * y0 - x0 * y2 - x1 * y0 - x2 * y1) / 2;
            if(s < 0)
                s = -1 * s;
            printf("%.1f
    ", s);
            cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;        
        }
        return 0;
    }

    #########################################################################

    二、输入三角形三个顶点的坐标

      判断该输入顺序是逆时针还是顺时针

     纯解析几何题有木有!!!

    //已知三点坐标判断是逆时针输入还是顺时针输入
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int main()
    {
        int x1,y1,x2,y2,x3,y3,i=0;
        double a,b,c,s,p;
        cin>>x1>>y1>>x2>>y2>>x3>>y3;
        while(x1 || y1 || x2 || y2 || x3 || y3)
        {
            cout<<((x3-x1)*(y2-y1)/(x2-x1)+y1>y3)<<endl;
            cin>>x1>>y1>>x2>>y2>>x3>>y3;
        } 
        return 0;
    }

    更好的方法 —— 依然有向面积

    #include<iostream>
    #include<cstdio>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
        double x0, x1, x2, y0, y1, y2, s;
        cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;
        while(x0 || y0 || x1 || y1 || x2 || y2)
        {
            s = x0 * y1 + x1 * y2 + x2 * y0 - x0 * y2 - x1 * y0 - x2 * y1;
            if(s < 0)                  //顺时针 
                cout << 1 << endl;
            else                       //逆时针
                cout << 0 << endl; 
            cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;        
        }
        return 0;
    }
  • 相关阅读:
    VM环境安装Linux系统
    设置ShowDialog
    sqlserver同步表的脚本
    C#winform设置DateTimePicker的时间格式
    C#winform解析marc显示在datagridview中以及marc卡片显示
    C#实现图书馆程序导入ISO-2709格式(MARC)功能
    时间格式转换+固定字段加空格
    sqlserver 保留小数方法
    winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
    怎么查看那个端口被占用
  • 原文地址:https://www.cnblogs.com/fengyanlover/p/5051180.html
Copyright © 2020-2023  润新知