I have to implement the following methods of class Segment:
Constructor with start and end points as parameters
Ensure that a created segment exists and is not degenerative which means that the start and the end of the segment is not the same point.
If it is, use throw new IllegalArgumentException() to raise an error.
double length()
Return length of the segment.
Point middle()
Return a middle point of the segment.
Point intersection(Segment another)
Return a point of the intersection of the current segment and the given one.
Return null if there is no such point.
Return null if segments are collinear.
Please, note that intersection point must lay on both segments.
Class Point is already there.
Examples
You may use Main class to try your code. There are some examples below.
Sample code:
...
double length = new Segment(new Point(0, 0), new Point(3, 4)).length();
System.out.println(length);
Output:
5
Sample code:
...
Segment first = new Segment(new Point(0, 0), new Point(4, 4));
Segment second = new Segment(new Point(2, 0), new Point(0, 2));
Point intersection = first.intersection(second);
System.out.println(intersection.getX());
System.out.println(intersection.getY());
Output:
1
1
Sample code:
...
Segment segment = new Segment(new Point(2, 0), new Point(0, 2));
Point midpoint = segment.middle();
System.out.println(midpoint.getX());
System.out.println(midpoint.getY());
Output:
1
1
Sample code:
...
Segment first = new Segment(new Point(0, 0), new Point(4, 0));
Segment second = new Segment(new Point(2, 1), new Point(1, 2));
Point intersection = first.intersection(second);
System.out.println(intersection == null);
Output:
true
This is class Segment:
<pre>class Segment {
Point start;
Point end;
public Segment(Point start, Point end) {
if (start == null || end == null) {
throw new IllegalArgumentException("Arguments can't be null");
}
if (start.equals(end)) {
throw new IllegalArgumentException("The points must differ.");
}
this.start = start;
this.end = end;
}
double length() {
return sqrt(pow(end.getX() - start.getX(), 2) + pow(end.getY() - start.getY(), 2));
}
Point middle() {
return new Point((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
}
Point intersection(Segment another) {
double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
if (k1 == k2) return null;
double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
(another.end.getX() - another.start.getX());
double x = (b2 - b1) / (k1 - k2);
double y = (k1 * b2 - k2 * b1) / (k1 - k2);
if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
(x < start.getX() && x < end.getX()) || (x < another.start.getX() && x < another.end.getX()) ||
(y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
(y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
return null;
return new Point(x, y);
}
}
This is class Point:
class Point {
private double x;
private double y;
public Point(final double x, final double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
And this is the main class:
public class Main {
public static void main(String[] args) {
{
double length = new Segment(new Point(0, 0), new Point(3, 4)).length();
System.out.println(length);
}
{
Segment segment = new Segment(new Point(2, 0), new Point(0, 2));
Point midpoint = segment.middle();
System.out.println(midpoint.getX());
System.out.println(midpoint.getY());
}
{
Segment first = new Segment(new Point(0, 0), new Point(4, 4));
Segment second = new Segment(new Point(2, 0), new Point(0, 2));
Point intersection = first.intersection(second);
System.out.println(intersection.getX());
System.out.println(intersection.getY());
}
{
Segment first = new Segment(new Point(0, 0), new Point(4, 0));
Segment second = new Segment(new Point(2, 1), new Point(1, 2));
Point intersection = first.intersection(second);
System.out.println(intersection == null);
}
}
}
My result are:
5.0
1.0
1.0
1.0
1.0
true
What I have tried:
But i need to return them integers as in examples. I need to change only the code from the class Segment.
I tried like this, adding (int) at every return, in order to return me integers, but it useless. I still have the same result.
Class Segment after changes:
<pre>class Segment {
Point start;
Point end;
public Segment(Point start, Point end) {
if (start == null || end == null) {
throw new IllegalArgumentException("Arguments can't be null");
}
if (start.equals(end)) {
throw new IllegalArgumentException("The points must differ.");
}
this.start = start;
this.end = end;
}
double length() {
return sqrt((int) pow(end.getX() - start.getX(), 2) + (int) pow(end.getY() - start.getY(), 2));
}
Point middle() {
return new Point((int) (start.getX() + end.getX()) / 2, (int) (start.getY() + end.getY()) / 2);
}
Point intersection(Segment another) {
double k1 = (end.getY() - start.getY()) / (end.getX() - start.getX());
double k2 = (another.end.getY() - another.start.getY()) / (another.end.getX() - another.start.getX());
if (k1 == k2) return null;
double b1 = (start.getY() * end.getX() - end.getY() * start.getX()) / (end.getX() - start.getX());
double b2 = (another.start.getY() * another.end.getX() - another.end.getY() * another.start.getX()) /
(another.end.getX() - another.start.getX());
double x = (b2 - b1) / (k1 - k2);
double y = (k1 * b2 - k2 * b1) / (k1 - k2);
if ((x > start.getX() && x > end.getX()) || (x > another.start.getX() && x > another.end.getX()) ||
(x < start.getX() && x < end.getX()) || (x < another.start.getX() && x < another.end.getX()) ||
(y > start.getY() && y > end.getY()) || (y > another.start.getY() && y > another.end.getY()) ||
(y < start.getY() && y < end.getY()) || (y < another.start.getY() && y < another.end.getY()))
return null;
return new Point((int) x, (int) y);
}
}
Output
5.0
1.0
1.0
1.0
1.0
true
Does anyone know hot to convert the double in int in order to have this results:
5
1
1
1
1
true