I have ordered points(x,y) for a route and i got a method optimize which removes consecutive points which distance between them is less than delta (delta=3).
The first rule tells that if p1 and p2 distances are lower than delta, then remove p2 from my route.
Second rule is same as first that i must remove the p2 point from route and add to p1 x coordinate: (p2.x - p1.x) / 2 and to p1 y coordinate (p2.y - p1.y) / 2 or in other words move old p1 in the center of p1 and p2.

import java.util.ArrayList;

public abstract class Route extends Point {
	
	public ArrayList<Point> points = new ArrayList<Point>();
	public ArrayList<Point> getPoints() {
		return points;
	}
	
	public final double delta = 3.0;
	
	abstract boolean optimize(Point p);
	
	public void toRemove() {
		for(Point p : points) {
			if ( optimize(p) ) removePoint(p);
		}
	}
	
	public void addPoint(Point p) {
		points.add(p);
	}
	public void removePoint(Point p) {
		points.remove(p);
	}
	
	public String toString(){
		 String s = "";
		 for(int i= 0; i<points.size();i++)
			 s +=(i+1) + " point: " + points.get(i) + "\n";
		 return s;
	   }
	
	public String getIndexValue(int i){
		   String s = points.get(i).toString();
		   return s;
	 }
	
	public static void main(String[] args){
		Route r;
		r = new Rule1();
		r.addPoint(new Point(10.0, 1.0));
		r.addPoint(new Point(1.0, 1.0));
		r.addPoint(new Point(3.0, 1.0));
		r.addPoint(new Point(4.0, 1.0));
		r.addPoint(new Point(-20.0, 1.0));
		//r.toRemove();
		System.out.println(r);
	}
}
public class Point {
	
	private double rho;
	private double theta;
	
	public Point(){
		rho = 0.0;
		theta = 0.0;
	}
		
	public Point(double x,  double y){
		setfromxy(x, y);
	}
	
	private void setfromxy(double x, double y){
		rho = Math.sqrt(x*x + y*y);
		theta = Math.atan2(y, x);
	}
	
	public double getx(){
		return getrho() * Math.cos(gettheta());
	}
	
	public double gety(){
		return getrho() * Math.sin(gettheta());
	}
	
	public double getrho(){
		return rho; 
	}
	
	public double gettheta(){
		return theta;
	}
	
	public Point vectorTo(Point p){
		return new Point(p.getx() - getx(), p.gety() - gety());
	}
	
	public double distance(Point p) {
		return vectorTo(p).getrho(); 
	}
	
	public void translate(double dx, double dy){
		double x = getx() + dx;
		double y = gety() + dy;
		setfromxy(x, y);
	}
	
	public void scale(double factor){
		rho *= factor;
	}
	
	public void centre_rotate(double angle){
		theta += angle;
	}
	
	public void rotate(Point p, double angle){
		translate(-p.getx(), -p.gety());
		centre_rotate(angle);
		translate(p.getx(), p.gety());
	}
}
public class Rule1 extends Route {

	@Override
	boolean optimize(Point p) {
		boolean lessThanDelta = false;
		if (this.distance(p) < delta){
			lessThanDelta = true;
		}
		return lessThanDelta;
	}

}
public class Rule2 extends Route{
	
	@Override
	boolean optimize(Point p) {
		boolean lessThanDelta = false;
		if (this.distance(p) < delta) {
			this.getx() = this.getx() + ((p.getx() - this.getx()) / 2);
			this.gety() = this.gety() + ((p.gety() - this.gety()) / 2);
			lessThanDelta = true;
		}	
		return lessThanDelta;
	}
}

I've looked at the examples in internet about comparing and there is this keyword then comes comparing function and in brackets is the object with which i compare.
As you see i have points arraylist which is my route, i'm iterating over it and the method optimize is doing the comparing and if it's true then removePoint method is called.
But in my example even first rule doesn't work and when running it says ConcurrentModificationException.

Hi

The part of code that gives the error is this:

public void toRemove() {
    for(Point p : points) {
        if ( optimize(p) ) removePoint(p);
    }
}

public void removePoint(Point p) {
    points.remove(p);
}

Use an iterator and Iterator.remove() instead of for each loop.

Hope it helps.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Hello moutanna, thank you for answering my questions.
I'm trying to do it now as you suggested. Added

Iterator<Point> itr = points.iterator();

and changed toRemove method to

public void toRemove() {
		while (itr.hasNext()) {
			if (optimize((Point) itr)) {
				itr.next();
				itr.remove();
			}
		}
	}

now program is throwing ClassCastException on this line: if (optimize((Point) itr))

Hi maybe here:

if (optimize((Point) itr)) {

you mean

if (optimize((Point) itr.next)) {

Please check the javaDoc's Iterator.
Hope it helps.

hi, thanks for correction, now it's

public void toRemove() {
		while (itr.hasNext()) {
			if (optimize((Point) itr.next())) {
				itr.remove();
			}
		}
	}

i get same error when i did with for each
at java.util.AbstractList$Itr.checkForComodification(Unknown at java.util.AbstractList$Itr.next(Unknown Source)
i'm not sure if my Rule class is correct

public class Rule1 extends Route {

	@Override
	boolean optimize(Point p) {
		boolean lessThanDelta = false;
		if (this.distance(p) < delta){
			lessThanDelta = true;
		}
		return lessThanDelta;
	}

}

im not sure if this.distance(p) takes p1.distance(p2) ????

ahhh, my lector said that

this.distance(p) < delta

here this is not my p1, but it's my arraylist points... so im trying to calculate points.distance(single point) but its impossible hats why it says ConcurrentModificationException.
but i don't get how this is my points arraylist, how does it point there, any1 cares to explain?
distance method should be called in my Route class and in my Rules class should be only the optimization rule which i want to apply.
Now i have to recode alot.

"this" refers to whatever Object called the method you are currently in. If the method that you are currently in is a constructor method, then "this" refers to the Object that you are creating.

This article has been dead for over six months. Start a new discussion instead.