Smooth Line : Line « 2D Graphics « Android






Smooth Line

  
// Created by plusminus on 13:24:05 - 21.09.2008
//package org.andnav2.osm.util;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

class Util {
  public static final String DEBUGTAG = "OPENSTREETMAP";

  public static final String BASEPATH_ON_EXTERNAL_MEDIA = "andnav2/";

  public static final String SDCARD_SAVEDROUTES_PATH = "routes/";
  public static final String SDCARD_SAVEDTRACES_PATH = "traces/";
  public static final String SDCARD_SKYHOOKCACHE_PATH = "skyhookcache/";
  public static final String SDCARD_TILE_PATH = "tiles/";

  public static final int NOT_SET = Integer.MIN_VALUE;

  public static final int Y = 0;
  public static final int X = 1;


  /**
   * 
   * @param pOrderedPoints Ordered means that the points are placed as if they are a line.
   */
  public static void smoothLine(final Set<ValuePair> pPointsInLineorder){
    ValuePair prev = null;
    ValuePair cur;

    /* We want only uniqe new values. */
    final Set<ValuePair> newPoints = new TreeSet<ValuePair>(new Comparator<ValuePair>(){
      @Override
      public int compare(final ValuePair a, final ValuePair b) {
        return a.compareTo(b);
      }
    });

    for(final Iterator<ValuePair> lineIterator = pPointsInLineorder.iterator(); lineIterator.hasNext(); ) {
      cur = lineIterator.next();

      if(prev != null){
        final int difA = cur.a - prev.a;
        final int difB = cur.b - prev.b;
        /* Check if we have a diagonal jump, like: (See X's)
         * 
         *      X----
         * ----X
         * 
         * */
        if(Math.abs(difA) == 1 && Math.abs(difB) == 1){
          /* Add two new points (See O's)
           * 
           *     0X----
           * ----X0
           * 
           */
          newPoints.add(new ValuePair(cur.a, prev.b));
          newPoints.add(new ValuePair(prev.a, cur.b));
        }
      }

      prev = cur;
    }

    pPointsInLineorder.addAll(newPoints);
  }}
class ValuePair implements Comparable<ValuePair>{
  //          
  // Constants
  //          

  //          
  // Fields
  //          

  final int a,b;

  //          
  // Constructors
  //          

  public ValuePair(final int[] reuse) {
    this.a = reuse[0];
    this.b = reuse[1];
  }

  public ValuePair(final int pA, final int pB) {
    this.a = pA;
    this.b = pB;
  }

  //          
  // Getter & Setter
  //          

  public int[] toArray() {
    return new int[]{this.a,this.b};
  }

  public int getValueA() {
    return this.a;
  }

  public int getValueB() {
    return this.b;
  }

  //          
  // Methods from SuperClass/Interfaces
  //          

  @Override
  public boolean equals(final Object o) {
    return o instanceof ValuePair
    && this.a == ((ValuePair)o).a
    && this.b == ((ValuePair)o).b;
  }

  @Override
  public int compareTo(final ValuePair another) {
    if(this.a != another.a) {
      return this.a - another.a;
    } else if(this.b != another.b) {
      return this.b - another.b;
    } else {
      return 0;
    }
  }

  //          
  // Methods
  //          
}

   
    
  








Related examples in the same category

1.Draw a line
2.Raster Line