Question

Write function to check if point O is located inside or outside of the 2D triangle...

Write function to check if point O is located inside or outside of the 2D triangle ABC The coordinates of the vertices are defined inside Coordinates.dat file. Read this file inside main m-file and provide the coordinates as an input to the function. Also ask user to input coordinates of the point O and provide it as input to the function as well. The output of the function should be a message, saying that the point is located inside or outside of the triangle. Hint: point O split ABC into small triangles

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package abcSolution1;

public class Triangle2D {

MyPoint p1;

MyPoint p2;

MyPoint p3;

public MyPoint getP1()

{

return p1;

}

public void setP1(MyPoint p1)

{

this.p1 = p1;

}

public MyPoint getP2()

{

return p2;

}

public void setP2(MyPoint p1)

{

this.p2 = p2;

}

public MyPoint getP3()

{

return p3;

}

public void setP3(MyPoint p3)

{

this.p3 = p3;

}

Triangle2D()

{

p1 = new MyPoint(0,0);

p2 = new MyPoint(1,1);

p3 = new MyPoint(2,5);

}

Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3)

{

this.p1 = p1;

this.p2 = p2;

this.p3 = p3;

}

public double getArea(){

double side1 = Math.sqrt(Math.pow(p2.getX()

-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));

double side2 = Math.sqrt(Math.pow(p3.getX()

-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));

double side3 = Math.sqrt(Math.pow(p1.getX()

-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));

double s =(side1 + side2 + side3)/2;

double area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));

return area;

}

public double getArea(MyPoint p1, MyPoint p2, MyPoint p3){

double side1 = Math.sqrt(Math.pow(p2.getX()

-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));

double side2 = Math.sqrt(Math.pow(p3.getX()

-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));

double side3 = Math.sqrt(Math.pow(p1.getX()

-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));

double s =(side1 + side2 + side3)/2.0;

double area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));

return area;

}

public boolean contains(MyPoint p){

// if point p is inside traingle ABC then

// area of pab+pbc+pca should equal to area of abc

if(Math.round(getArea(p, p1, p2)+getArea(p, p2, p3)+getArea(p, p3, p1))==Math.round(getArea(p1, p2, p3)))

return true;

return false;

}

}

package abcSolution1;

public class MyPoint {

double x;

double y;

public MyPoint(double x, double y) {

super();

this.x = x;

this.y = y;

}

public MyPoint() {

// TODO Auto-generated constructor stub

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

public double distance(MyPoint point) {

return Math.sqrt(Math.pow(point.getX()-this.getX(),2)+Math.pow(point.getY()-this.getY(),2));

}

}

package abcSolution1;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class TriangleDriver {

public static void main(String[] args) {

File file = new File("Coordinates.dat");

try {

Scanner sc=new Scanner(file);

String line1=sc.nextLine();

String line2=sc.nextLine();

String line3=sc.nextLine();

MyPoint point1=new MyPoint(Double.valueOf(line1.split(" ")[0]), Double.valueOf(line1.split(" ")[1]));

MyPoint point2=new MyPoint(Double.valueOf(line1.split(" ")[0]), Double.valueOf(line1.split(" ")[1]));

MyPoint point3=new MyPoint(Double.valueOf(line1.split(" ")[0]), Double.valueOf(line1.split(" ")[1]));

sc=new Scanner(System.in);

System.out.println("Enter x coordinate of point o");

double x=sc.nextDouble();

System.out.println("Enter y coordinate of point o");

double y=sc.nextDouble();

MyPoint o=new MyPoint(x, y);

Triangle2D triangle = new Triangle2D(point1, point2, point3);

System.out.println(triangle.contains(o));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Add a comment
Know the answer?
Add Answer to:
Write function to check if point O is located inside or outside of the 2D triangle...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • C++ Hey, I really need help with this lab. I don't understand the function part at...

    C++ Hey, I really need help with this lab. I don't understand the function part at all. It'll be great way for me to understand. Than you Functions and loops: Write a program that will ask the user which type of triangle they would like to draw on the characters. The choices are screen with left-top-big left-bottom-big right-top-big right-bottom-big Get the user's choice, ask how big it should be, and then draw the shape. After each successful drawing, ask the...

  • 11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...

    11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single user-defined function that analyzes a 2D array of chars. You do NOT need to write any of the code in main0 that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win0 with the following definition (i.e. prototype): int win(char board17)15), char player) The function win0 should return a 1 if the character...

  • ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the...

    ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the file is directly executed Hint: def main(): pass if __name__ == '__main__': main() 2. Your programs will use a file called scores.txt to store a bunch of user-provided numbers. These can be int or float but their values must be between 0 and 100. Inside the main function, display the current list of numbers to the user and ask if they want to update...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...

    need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after the decimal point. Process and sample run: a) Function 1: A void function that uses parameters passed by reference representing the name of a data file to read data from, amount of principal in an investment portfolio, interest rate (any number such as 5.5 for 5.5% interest rate, etc.) , number of times the interest is compounded, and the number of years the money is...

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT