1. Write a program that reads two coordinates (x1, y1) and (x2, y2), and prints
ID: 3639741 • Letter: 1
Question
1. Write a program that reads two coordinates (x1, y1) and (x2, y2), and prints the slope of the line passing through these two points.2. Write a program that reads three coordinates, (x1, y1), (x2, y2), (x3, y3) and does the following:
(a) It checks whether the three points are collinear
(b) It prints the coordinates of the bounding box (the smallest rectangle whose edges are parallel to the coordinate axes and which contains all three points)
(c) It checks whether the triangle formed by the three points is a right angled triangle
(d) It prints the area of the triangle
3. Modify the program of part-2 so that the program reads the three coordinates from a file asg1.in and prints the output in the file asg1.out
Explanation / Answer
#include<stdio.h>
#include<math.h>
main()
{
float x1,x2,x3,y1,y2,y3,s1,s2,s3,A,s,x,a,b,c,a1,a2,a3,a4,b1,b2,b3,b4;
printf("Enter the coordinates of first point:");
scanf("%f%f",&x1,&y1);
printf(" Enter the coordinates of second point:");
scanf("%f%f",&x2,&y2);
printf(" Enter the coordinates of third point:");
scanf("%f%f",&x3,&y3);
s1=(y2-y1)/(x2-x1);
s2=(y3-y1)/(x3-x1);
s3=(y3-y2)/(x3-x2);
if ( x1==x2 || x2==x3 || x1==x3 || s1==s2)
{
printf("The given points are collinear");
printf("The area is zero");
}
else
{
printf("Not collinear");
a=sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
b=sqrt(((x3-x2)*(x3-x2))+((y3-y2)*(y3-y2)));
c=sqrt(((x3-x1)*(x3-x1))+((y3-y1)*(y3-y1)));
s=(a+b+c)/2;
x=s*(s-a)*(s-b)*(s-c);
A=sqrt(x);
printf(" The area of triangle formed by given points is %f",A);
}
if( (s1*s2)==-1 || (s1*s3)==-1 || (s2*s3)==-1 )
{
printf(" The given points form a right angled triangle.");
}
else
{
printf(" They dont form a right triangle");
}
a2=( x1>x2 && x1>x3 ) ? x1:( x2>x3 && x2>x1 ) ? x2:x3;
a1=a2;
b2=( y1>y2 && y1>y3 ) ? y1:( y2>y3 && y2>y1 ) ? y2:y3;
b3=b2;
a3=( x1<x2 && x1<x3 )? x1:( x2<x3 && x2<x1 )? x2:x3;
a4=a3;
b1=( y1<y2 && y1<y3 ) ? y1:(y2<y3 && y2<y1) ? y2:y3;
b1=b4;
printf("The coordinates of the bounding box are (%f,%f),(%f,%f),(%f,%f),(%f,%f)",a1,b1,a2,b2,a3,b3,a4,b4);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.