Saturday 11 June 2016

Chapter 3 Exercise 27, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**3.27 (Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle. Here are the sample runs:

Enter a point's x- and y-coordinates: 100.5 25.5
The point is in the triangle Enter a point's x- and y-coordinates: 100.5 50.5
The point is not in the triangle 



import java.util.Scanner;
 
public class ProgrammingEx3_27 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter a point's x- and y-coordinates:");
  double x = input.nextDouble();
  double y = input.nextDouble();
  double y2 = -x / 2 + 100;
  String s = " ";
 
  // Check if y and x is in range and under the line
 if(( (y > 0) && (x > 0) && (x + 2*y < 200) )){
   s = " ";
  }
  else
  {
    s = " not ";
  }
 
  System.out.print("The point is" + s + "in the triangle");
 
 }
}

4 comments :

  1. @zakaria hashi thank you for pointing out mistake. I really appreciate it and also providing me with test input so i can improve the solutions

    ReplyDelete
    Replies
    1. Well done, it's really a hard one i couldn't even think of it like that.

      Delete
  2. more expliantion
    https://stackoverflow.com/questions/14669614/finding-whether-a-point-is-within-a-triangle

    ReplyDelete