#### Question Details

Define three classes to represent sloped line, horizontal line, and

vertical line and they must implement the Line interface.

Implement distanceTo(Point p) method for the three classes so that

it returns the shortest distance from the line to the point p. Also implement

nearestPointTo(Point p) to return the nearest point on this line to p.

------------------------------------------------------------------------------------------------------

class Point {

final double x,y; // x, y coordinate of a point

Point(int x, int y) {

this.x = x;

this.y = y;

}

double distanceTo(Point p) {

return Math.sqrt(Math.pow(x-p.x, 2) + Math.pow(y-p.y, 2));

}

}

interface Line {

Point nearestPointTo (Point p);

double distanceTo (Point p);

}

// Define class SlopedLine, HorizontalLine, and VerticalLine

// that implement Line interface

// HorizontalLine only needs to know the y coordinate of the points

// VerticalLine only needs to know the x coordinate of the points

// SlopedLine will store its slope m and intercept k

// Make sure you also define a method "Point nearestPointTo(Point p)"

// to return the nearest point on this line to p

-----------------------------------------------------------------------------------------------------

The two points for a horizontal line has the same y coordinates. The shortest

distance from the horizontal line to a point is the difference between their y

coordinates. The two points for a vertical line has the same x coordinates. The

shortest distance from the vertical line to a point is the difference between their

x coordinates.

Given points p1 and p2 for a sloped line, you will and its slope m and

intercept k by

m =p1.y - p2.y / p1.x- p2.x

k = p1.y - m * p1.x

You should store the slope and intercept in the fields of the sloped line object.

Given a line defined by slope m and intercept k, you can finnd its normal line

l that passes the point p by

ml = 1=m

kl = p.y - ml * p:x

where ml and kl is the slope and intercept of the normal line l.

Next step is to find the intersection point pl between this line and its normal

line l. The x, y coordinates of pl is defined by

x = (l.k - k)=(m - l.m)

y = m * x + k

The smallest distance from this line to the point p is the distance between pl to p. Also, pl is the nearest point to p.

----------------------------------------------------------

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class Hwk5 {

public class Hwk5 {

@Test

public void testSlopedLine1() {

Point p = new Point(2,3);

Line l1 = makeLine(new Point(1,1), new Point(2,2));

assertTrue(l1.distanceTo(p) == Math.sqrt(2)/2);

assertTrue(l1.nearestPointTo(p).equals(new Point(2.5, 2.5)));

}

@Test

public void testSlopedLine2() {

Point p = new Point(3,3);

Line l1 = makeLine(new Point(2,3), new Point(3,4));

assertTrue(l1.distanceTo(p) == Math.sqrt(2)/2);

assertTrue(l1.nearestPointTo(p).equals(new Point(2.5, 3.5)));

}

@Test

public void testHorizontalLine() {

Point p = new Point(2,2);

Line l1 = makeLine(new Point(1,1), new Point(3,1));

assertTrue(l1.distanceTo(p) == 1);

assertTrue(l1.nearestPointTo(p).equals(new Point (2, 1)));

}

@Test

public void testVerticalLine() {

Point p = new Point(2,1);

Line l1 = makeLine(new Point(1,0), new Point(1,2));

assertTrue(l1.distanceTo(p) == 1);

assertTrue(l1.nearestPointTo(p).equals(new Point(1, 1)));

}

Line makeLine(Point p1, Point p2) {

Line ret;

if (p1.x == p2.x) {

ret = new VerticalLine(p1.x);

}

else if (p1.y == p2.y) {

ret = new HorizontalLine(p1.y);

}

else {

ret = new SlopedLine(p1, p2);

}

return ret;

}

}

class Point {

final double x, y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

public double distanceTo(Point p) {

return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));

}

public String toString() { return "(" + x + ", " + y + ")"; }

public boolean equals(Object that) {

boolean ret = false;

if(that instanceof Point) {

Point thatPoint = (Point) that;

ret = x == thatPoint.x && y == thatPoint.y;

}

return ret;

}

}

interface Line {

double distanceTo(Point p);

Point nearestPointTo(Point p);

}

/*

* Define class SlopedLine, HorizontalLine, and VerticalLine that implement Line interface

*

* HorizontalLine only needs to know the y coordinate of the points

*

* VerticalLine only needs to know the x coordinate of the points

*

* SlopedLine will store its slope m and intercept k in its fields

*

* Make sure that you also define the method 'Point nearestPointTo(Point p)'

* to return the nearest point on a line to p

*/

**Solution details:**

Answered

QUALITY

Approved

ANSWER RATING

This question was answered on: * Dec 08, 2020 *

* * Solution~000652147569896.zip (25.37 KB)

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free solution (Deadline assured. Flexible pricing. TurnItIn Report provided)

##### Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .

#### About this Question

STATUSAnswered

QUALITYApproved

DATE ANSWEREDDec 08, 2020

EXPERTTutor

ANSWER RATING

#### GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

- As a reference for in-depth understanding of the subject.
- As a source of ideas / reasoning for your own research (if properly referenced)
- For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).

#### NEW ASSIGNMENT HELP?

### Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions.
New orders are original solutions *and precise to your writing instruction requirements. Place a New Order using the button below.*

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN A DEADLINE.