# Answer to Question #43756 in Java | JSP | JSF for David

Question #43756

So the question is:

The goal is to remove all points outside this rectangle from the Cloud much like you would crop an image. The crop method must deal with two input points on a horizontal or vertical line segment, in which case all points not on the line segment are removed, and it must deal with two equal Points p1 and p2, in which case all Points but p1 are removed from the Cloud.

So say for example I have a cloud of:

[(3.0,1.0), (2.0,2.0), (1.5,1.5), (3.0,0.0)]

and I call my crop method on p1,p4 I should come up with:

[(3.0,1.0), (3.0,0.0)]

I have a solution how would somoene else go about solving it(most efficent way possible)

The goal is to remove all points outside this rectangle from the Cloud much like you would crop an image. The crop method must deal with two input points on a horizontal or vertical line segment, in which case all points not on the line segment are removed, and it must deal with two equal Points p1 and p2, in which case all Points but p1 are removed from the Cloud.

So say for example I have a cloud of:

[(3.0,1.0), (2.0,2.0), (1.5,1.5), (3.0,0.0)]

and I call my crop method on p1,p4 I should come up with:

[(3.0,1.0), (3.0,0.0)]

I have a solution how would somoene else go about solving it(most efficent way possible)

Expert's answer

`import java.awt.Point;`

import java.util.ArrayList;

public class CropPoint {

ArrayList<Point> points;

public static void main(String[] args) {

CropPoint a = new CropPoint();

a.run();

}

public CropPoint(){

points = new ArrayList<Point>();

}

public void run(){

points.add(new Point());

points.get(0).setLocation(1.0, 1.5);

points.add(new Point());

points.get(1).setLocation(0.0, 1.5);

points.add(new Point());

points.get(2).setLocation(0.0, 1.5);

points.add(new Point());

points.get(3).setLocation(0.0, 1.5);

System.out.println("Points before cropping");

for (Point p : points){

System.out.println("x: "+p.getX()+"\ty: "+p.getY());

}

Crop(points.get(1), points.get(2));

System.out.println("Points after cropping");

for (Point p : points){

System.out.println("x: "+p.getX()+"\ty: "+p.getY());

}

}

private void Crop(Point p1, Point p2){

if (p1.getX() == p2.getX() && p1.getY() == p2.getY()){

for (int i = 0; i < points.size(); i++){

if (points.get(i) != p1){

points.remove(i);

i = 0;

}

}

}

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

for (int i = 0; i < points.size(); i++){

if (points.get(i).getX() != p1.getX()){

points.remove(i);

i = 0;

}

}

}

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

for (int i = 0; i < points.size(); i++){

if (points.get(i).getY() != p1.getY()){

points.remove(i);

i = 0;

}

}

}

else{

System.out.println("Error input data");

}

}

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment