Write a Java program to read three points that can form a triangle and then to f
ID: 3754781 • Letter: W
Question
Write a Java program to read three points that can form a triangle and then to find the smallest upright rectangle containing the triangle. use non advanced methods bacsue i am a ne students and just trying to learn the basics.
this is what i have so far,
import java.util.Scanner;
public class HW2 {
public static void main(String[] args) {
Scanner in = new Scanner ( System.in);
double x1, x2, x3, y1, y2, y3;
System.out.println("Enter first point of triangle (x,y): ");
x1 = in.nextDouble();
y1 = in.nextDouble();
System.out.println("Enter second point of triangle (x,y) : ");
x2 = in.nextDouble();
y2 = in.nextDouble();
System.out.println("Enter third point of triangle (x,y): ");
x3 = in.nextDouble();
y3 = in.nextDouble();
Explanation / Answer
1.
// Function to calculate for validity
public static int checkValidity(int a,
int b, int c)
{
// check condition
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
// Driver function
public static void main(String args[])
{
int a = 7, b = 10, c = 5;
// function calling and print output
if ((checkValidity(a, b, c)) == 1)
System.out.print("Valid");
else
System.out.print("Invalid");
}
}
2.
import java.util.*;
class GFG {
/* A utility function to calculate area of triangle
formed by (x1, y1) (x2, y2) and (x3, y3) */
static double area(int x1, int y1, int x2, int y2,
int x3, int y3)
{
return Math.abs((x1*(y2-y3) + x2*(y3-y1)+
x3*(y1-y2))/2.0);
}
/* A function to check whether point P(x, y) lies
inside the triangle formed by A(x1, y1),
B(x2, y2) and C(x3, y3) */
static boolean isInside(int x1, int y1, int x2,
int y2, int x3, int y3, int x, int y)
{
/* Calculate area of triangle ABC */
double A = area (x1, y1, x2, y2, x3, y3);
/* Calculate area of triangle PBC */
double A1 = area (x, y, x2, y2, x3, y3);
/* Calculate area of triangle PAC */
double A2 = area (x1, y1, x, y, x3, y3);
/* Calculate area of triangle PAB */
double A3 = area (x1, y1, x2, y2, x, y);
/* Check if sum of A1, A2 and A3 is same as A */
return (A == A1 + A2 + A3);
}
/* Driver program to test above function */
public static void main(String[] args)
{
/* Let us check whether the point P(10, 15)
lies inside the triangle formed by
A(0, 0), B(20, 0) and C(10, 30) */
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
System.out.println("Inside");
else
System.out.println("Not Inside");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.