Write a program that, for four points A, B, C and P, which a) Draws a triangle formed by ABC and a small cross showing the position of P b) Displays a line of text indicating which of the following three cases applies: P lies • Inside ABC • Outside ABC • On an edge of ABC. The user will specify the four points by clicking.

Respuesta :

Answer:

import java.awt.*;

import java.applet.*;

public class DrawTrainlge extends Applet{

static double findDistance(float x1, float y1,float a, float b,float c){

double dis = Math.abs(((a * x1 + b * y1 + c)) /  (Math.sqrt(a * a + b * b)));

return dis;

}

public void paint(Graphics g){

Point[] point = new Point[4];

g.drawLine(point[0].x , point[0].y , point[1].x , point[1].y);

g.drawLine(point[0].x , point[0].y , point[2].x , point[2].y);

g.drawLine(point[2].x , point[2].y , point[1].x , point[1].y);

double newdistance = 9999999;

for(int i=0;i<3;i++){

            int first = (i+1)%3;

            int second = i;

            float a = point[first].y - point[second].y;

            float b = point[second].x - point[first].x;

            float c = a*(point[second].x) + b*(point[second].y);

            double dis = findDistance(point[3].x,point[3].y,a,b,c);

            if(newdistance > dis){

               dis = newdistance;

         }

}

g.drawString("Shortest Distance");

g.drawString(dis);

}

}

Explanation:

  • Inside the findDistance method, find the distance  by using the following formula:

Math.abs(((a * x1 + b * y1 + c)) /  (Math.sqrt(a * a + b * b)))

  • Inside the paint method, give your input in the form of four points(A,B,C,D) .
ACCESS MORE