C++ 将三角形定义为具有坐标的数据类型。计算三角形的内容

C++ 将三角形定义为具有坐标的数据类型。计算三角形的内容,c++,math,C++,Math,我是编程新手,但我正在努力变得更好,以下是我试图解决的问题 我需要定义顶点位于正方形(0,1)x(0,1)内的数据类型三角形。编写一个计算其面积的函数。编写一个程序,生成100x个三元组的x,y坐标对作为十进制数,并计算生成的三角形的平均面积 这听起来可能很难,但事实并非如此,我想我知道该怎么做,但我只是不知道如何写下来。 我需要计算随机坐标的距离。然后将内容与所有100个三角形的内容相加,并将其除以100 我所拥有的是: #include <iostream> #include &

我是编程新手,但我正在努力变得更好,以下是我试图解决的问题

我需要定义顶点位于正方形(0,1)x(0,1)内的数据类型三角形。编写一个计算其面积的函数。编写一个程序,生成100x个三元组的x,y坐标对作为十进制数,并计算生成的三角形的平均面积

这听起来可能很难,但事实并非如此,我想我知道该怎么做,但我只是不知道如何写下来。 我需要计算随机坐标的距离。然后将内容与所有100个三角形的内容相加,并将其除以100

我所拥有的是:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
    srand(time(NULL));
    
    int x, y;
    int sizeOfSquare=10;
    int triangle(a,b,c); //sides of triangle
    
    int arrX[3];
    int arrY[3];
    
    
    for(int i=0; i<3; i++)
    {
        x = rand()%sizeOfSquare+1;
        y = rand()%sizeOfSquare+1;
        
        arrX[i] = x-1;
        arrY[i] = y-1;
        
        //-1 to get a chance of zero
    }
    
    for (int i=0;i<3; i++)  //this is just for me to see the coordinates
        cout << "(" << arrX[i] << "," << arrY[i] << ")" << endl;
    
    
return 0;   
}  
#包括
#包括
#包括
使用名称空间std;
int main()
{
srand(时间(空));
int x,y;
int sizeOfSquare=10;
int三角形(a,b,c);//三角形的边
int-arrX[3];
国际协议[3];

对于(inti=0;i我认为您的思路是正确的,但在解决此问题之前,您还需要一些工具

首先,它讨论了
数据类型
:您需要一种方法来存储每个三角形的信息。有几种方法可以做到这一点,您需要了解
struct
s

这一行代码没有达到您预期的效果:

 int triangle(a,b,c); //sides of triangle
这是一个“函数声明”,不允许存储或定义“数据类型”

使用数组存储坐标是一个好主意(可能比您的级别要复杂一点,但我认为您会处理得很好)。您可以简化“+1”和“-1”以生成这些坐标

其中的关键部分是创建正确的数据类型。 你需要一种方法来存储每个三角形的数据。然后每个三角形也必须被存储。你已经让它在一个三角形中工作得很好了,一旦你知道如何存储每个三角形,找到几个三角形就会更容易

一旦有了这些三角形,就可以更容易地找到每个三角形的面积。 在这一点上,将它们存储在数组中并进行汇总对您来说应该是显而易见的

希望这能帮助你,不要担心合适的解决方案和更好的解决方案。 首先让它工作,并确保您首先理解所有内容!

\include
#include<iostream>
#include<cmath>

using namespace std;

struct point     //we define a point as a structure which contains x coordinate and y coordinate
{
    double x;
    double y;
};

class triangle
{
    point a,b,c;
    
    public:
    triangle()    //if you do not pass any points they all coordinates will be initialized to 01
    {
        a.x=0;a.y=0;
        b.x=0;b.y=0;
        c.x=0;c.y=0;
    }
    triangle(point A,point B,point C)   //if you pass points then this constructor(function) will be called
    {
        a=A;
        b=B;
        c=C;
    }
    
    double area()  //to calculate area of this triangle
    {
        
        double x1,y1,x2,y2,x3,y3;
        x1=a.x;y1=a.y;
        x2=b.x;y2=b.y;
        x3=c.x;y3=c.y;
        return (0.5)*abs((x1*y2 + x2*y3 + x3*y1) - (x2*y1 + x3*y2 + x1*y3));  // (1/2) x | (x₁ y₂ + x₂ y₃ + x₃ y₁) - (x₂ y₁ + x₃ y₂ + x₁ y₃) |
    }
};

int main()
{
    srand(time(NULL));
    int sizeOfSquare=10;
    // point a,b,c;
    // int triangle(a,b,c); //sides of triangle
    
    point Points[3];  //point is a data type now and we are creating an array of point and calling this variable as Points
    
    for(int i=0; i<3; i++)
    {
        Points[i].x = rand()%sizeOfSquare;
        Points[i].y = rand()%sizeOfSquare;
    }
    
    for (int i=0;i<3; i++)  //this is just for me to see the coordinates  //you version of code just modified a bit
        cout << "(" << Points[i].x << "," << Points[i].y << ") ";
      
    triangle t1(Points[0],Points[1],Points[2]);   //just checking if our class works
    cout<<"area of this triangle is "<<t1.area()<<endl;   //if area is correct it works, I have verified on this site https://www.triangle-calculator.com/?what=vc
    
    
    // Now we have to generate 100 triangles and calculate average of their areas
    int noOfTriangles=100;
    triangle Triangles[noOfTriangles];   //please note triangle is a data type now and you can pass three points to initialize it
    for(int i=0;i<noOfTriangles;++i)
    {
        for(int i=0; i<3; i++)   //since we have three points
        {
            Points[i].x = rand()%sizeOfSquare;     //calculate x coordinate of each point
            Points[i].y = rand()%sizeOfSquare;     //calculate y coordinate of each point
        }
        triangle temp(Points[0],Points[1],Points[2]);
        Triangles[i]=temp;
    }
    
    double totalArea;   // to hold sum of area  of all triangles
    for(int i=0;i<noOfTriangles;++i)
    {
        totalArea+=Triangles[i].area();
    }
    
    cout<<"Total Area of all "<<noOfTriangles<<" is "<<totalArea<<endl;
    cout<<"Average area is "<<totalArea/(double)noOfTriangles<<endl;
    
    return 0;
}
#包括 使用名称空间std; 结构点//我们将点定义为包含x坐标和y坐标的结构 { 双x; 双y; }; 阶级三角 { a、b、c点; 公众: 三角形()//如果不传递任何点,则所有坐标都将初始化为01 { a、 x=0;a.y=0; b、 x=0;b.y=0; c、 x=0;c.y=0; } 三角形(点A、点B、点C)//如果传递点,则将调用此构造函数(函数) { a=a; b=b; c=c; } double area()//用于计算此三角形的面积 { 双x1,y1,x2,y2,x3,y3; x1=a.x;y1=a.y; x2=b.x;y2=b.y; x3=c.x;y3=c.y; 返回值(0.5)*abs((x1*y2+x2*y3+x3*y1)-(x2*y1+x3*y2+x1*y3));/(1/2)x |(x)₁ Y₂ + x₂ Y₃ + x₃ Y₁) - (十)₂ Y₁ + x₃ Y₂ + x₁ Y₃) | } }; int main() { srand(时间(空)); int sizeOfSquare=10; //a、b、c点; //int三角形(a,b,c);//三角形的边 point Points[3];//point现在是一种数据类型,我们正在创建一个point数组,并将此变量称为Points
对于(int i=0;iYou应该从任务的第一句话开始:“将三角形定义为具有坐标的数据类型”。
int
当然不能容纳3个整数。