Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Solution to homework task
- * Introduction to programming course
- * Faculty of Mathematics and Informatics of Sofia University
- * Winter semester 2017/2018
- *
- * @author NATALIJA STOJANOVA
- * @idnumber 855301
- * @task 7
- * @compiler GCC
- *
- */
- #include<iostream>
- #include<cmath>
- using namespace std;
- int orientation(int ax, int ay, int bx, int by, int cx, int cy)
- {
- int dx1=bx-ax;
- int dy1=by-ay;
- int dx2=cx-ax;
- int dy2=cy-ay;
- return (dx1*dy2-dy1*dx2);
- }
- // intersect function finds if the side of first and second triangle intersect
- bool intersect(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy)
- {
- return ((orientation(ax, ay, cx, cy, dx, dy)*orientation(bx, by, cx, cy, dx, dy)<0) && (orientation(cx, cy, ax, ay, bx, by)*orientation(dx, dy, ax, ay, bx, by)<0));
- }
- int main ()
- {
- cout<<"THIS PROGRAM DEFINES THE KIND OF TRIANGLES AND MEASURE THE AREA OF CROSS SECTION OF THE TRIANGLES "<<endl;
- float a1x, a1y, b1x, b1y, c1x, c1y, a2x, a2y, b2x, b2y, c2x, c2y;
- //a1x and a1y defines the first vertex in first triangle
- //b1x and b1y defines the second vertex in first triangle
- //c1x and c1y defines the third vertex in first triangle
- //a2x and a2y defines the first vertex in second triangle
- //b2x and b2y defines the second vertex in second triangle
- //c2x and c2y defines the third vertex in second triangle
- cout<<"Enter the vertex of first triangle. Vertex A1"<<endl;
- cin>>a1x>>a1y;
- cout<<"Enter the vertex of first triangle Vertex B1"<<endl;
- cin>>b1x>>b1y;
- cout<<"Enter the vertex of first triangle Vertex C1"<<endl;
- cin>>c1x>>c1y;
- cout<<"Enter the vertex of second triangle. Vertex A2"<<endl;
- cin>>a2x>>a2y;
- cout<<"Enter the vertex of second triangle. Vertex B2"<<endl;
- cin>>b2x>>b2y;
- cout<<"Enter the vertex of second triangle. Vertex C2"<<endl;
- cin>>c2x>>c2y;
- bool flag11=0, flag12=0, flag21=0, flag22=0;
- // calculates the size of sides (a,b and c) with formula for distance between two points (first triangle)
- float a=sqrt((b1x-c1x)*(b1x-c1x)+(b1y-c1y)*(b1y-c1y)), b=sqrt((a1x-c1x)*(a1x-c1x)+(a1y-c1y)*(a1y-c1y)), c=sqrt((b1x-a1x)*(b1x-a1x)+(b1y-a1y)*(b1y-a1y));
- if (a==b && b==c)flag11=1; //Checks whether the triangle is equilateral
- // calculates the size of sides (a,b and c) with formula for distance between two points (second triangle)
- a=sqrt((b2x-c2x)*(b2x-c2x)+(b2y-c2y)*(b2y-c2y)), b=sqrt((a2x-c2x)*(a2x-c2x)+(a2y-c2y)*(a2y-c2y)), c=sqrt((b2x-a2x)*(b2x-a2x)+(b2y-a2y)*(b2y-a2y));
- if (a==b && b==c)flag12=1; //Checks whether the triangle is equilateral
- if(a1y==b1y){
- if(c1y>a1y)flag21=1; //Checks whether the triangle is upright
- if(a1x>b1x)swap(a1x, b1x); //sorts the vertexes
- }
- else if(a1y==c1y){
- if(b1y>a1y)flag21=1; //Checks whether the triangle is upright
- if(a1x>c1x)swap(a1x, c1x); //sorts the vertexes
- }
- else if(c1y==b1y){
- if(a1y>b1y)flag21=1; //Checks whether the triangle is upright
- if(b1x>c1x)swap(b1x, c1x); //sorts the vertexes
- }
- if(a2y==b2y){
- if(c2y>a2y)flag22=1; //Checks whether the triangle is upright
- if(a2x>b2x)swap(a2x, b2x); //sorts the vertexes
- }
- else if(a2y==c2y){
- if(b2y>a2y)flag22=1; //Checks whether the triangle is upright
- if(a2x>c2x)swap(a2x, c2x); //sorts the vertexes
- }
- else if(c2y==b2y){
- if(a2y>b2y)flag22=1; //Checks whether the triangle is upright
- if(b2x>c2x)swap(b2x, c2x); //sorts the vertexes
- }
- if (flag11&&flag21)
- cout<<"First triangle is upright and equilateral"<<endl;
- else cout<<"First triangle is not upright or equilateral"<<endl;
- if (flag12 && flag22)
- cout<<"Second triangle is upright and equilateral"<<endl;
- else cout<<"Second triangle is not upright or equilateral"<<endl;
- float x1, y1, x2, y2; //x1 and y1 defines the first vertex of new triangle. x2 and y2 defines the second vertex of new triangle
- //The cross section of the triangles is also a equilateral triangle
- float a1, b1, c1;
- float a2, a3, b2, b3, c2, c3;
- if(flag11 && flag12 && flag21 && flag22)
- {
- float dx1, dx2, dy2;
- if(a1y==b1y)
- {
- if(a2y==b2y && intersect(a1x, a1y, c1x, c1y, a2x, a2y, b2x, b2y))
- {
- a1=a1y-c1y, b1=c1x-a1x, c1=a1x*c1y-c1x*a1y, a2=a2y-b2y, b2=b2x-a2x, c2=a2x*b2y-b2x*a2y;
- x1=(-c1*b2+c2*b1)/(a1*b2-a2*b1);
- y1=(-a1*c2+a2*c1)/(a1*b2-a2*b1);
- if(intersect(b1x, b1y, c1x, c1y, a2x, a2y, b2x, b2y))
- {
- a3=a2y-b2y, b3=b2x-a2x, c3=a2x*b2y-b2x*a2y;
- x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
- y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
- }
- else if(intersect(b1x, b1y, c1x, c1y, a2x, a2y, c2x, c2y))
- {
- a3=b2y-c2y, b3=c2x-b2x, c3=b2x*c2y-c2x*b2y;
- x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
- y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
- }
- else if(intersect(a1x, a1y, c1x, c1y, b2x, b2y, c2x, c2y))
- {
- a3=b2y-c2y, b3=c2x-b2x, c3=b2x*c2y-c2x*b2y;
- x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
- y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
- }
- }
- }
- float Area;
- float Side_newTriagle = sqrt((x2-x1)*(x2-x1)-(y2-y1)*(y2-y1));
- Area = (sqrt(3)*Side_newTriagle*Side_newTriagle)/4;
- cout<<"The area of cross section of the triangles is: "<<Area;
- }
- else
- cout<<"The cross section of the triangles can not be measured because they are not uprighted and equilateral";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement