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.