Reputation: 1
I am new to programming but I am trying to get better, here is what I am trying to solve.
I need to define the data type triangle with vertices inside the square (0,1) x (0,1). Write a function that calculates its area. Write a program that generates 100x triples of x, y pairs of coordinates as decimal numbers and calculates the average area of the generated triangles.
It may sound difficult, but it is not, I think I know what to do but I just don't know how to write it down. I need to calculate the distance of random coordinates. Then sum up the content with content of all 100 triangles and divide it with 100.
What I have is this:
#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;
}
It generates random pairs of coordinates in arrays, but I don't know how to calculate the distance between the arrays and assign it to triangle.
Maybe there is a better solution, so sorry if my code is not proper. Thanks for any suggestions.
Upvotes: 0
Views: 552
Reputation: 453
#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;
}
OUTPUT:
(1,8) (9,5) (3,0) area of this triangle is 29
Total Area of all 100 is 707
Average area is 7.07
Link to the code https://onlinegdb.com/HJIwVW01P
Formula for area of triangle I got from here https://www.math-only-math.com/area-of-the-triangle-formed-by-three-co-ordinate-points.html
Please let me know if you have any issues in understanding any part of the code. I will be very happy to help.
Please note, the above program generates random integers and not decimal values. According to your question you need to generate decimal values, I have left that task to you.
Upvotes: 0
Reputation: 2533
I think you're on the right track, but you need several more tools before tackling this problem.
First of all it talks about data type
: you need a way to store information about each triangle. There are several ways to do this, and you'll need to learn about struct
s.
This line of code is not doing what you expect :
int triangle(a,b,c); //sides of triangle
This is a "function declaration", and doesn't allow to store or define "data-types".
Storing the coordinates with arrays is a good idea (maybe a tad more complicated than it should be at your level, but I think you'll manage all right). And you can probably simplify the "+1" and "-1" to generate those coordinates a bit.
The key part of this is to create the proper datatype. You need a way to store your data for each triangle. Then each triangle has to be also stored. You've got it working well enough for one triangle, finding for several will be easier once you figure how to store each triangle.
Once you have those triangles, finding the areas for each will be easier. Storing them in arrays and summing it up should be obvious to you at this point.
Hope this helps you out, and don't worry about proper solutions and better solutions. Make it work first, and make sure you understand it all at first!
Upvotes: 1