Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / chart / CubicLineChart.java
diff --git a/android-libraries/achartengine/src/org/achartengine/chart/CubicLineChart.java b/android-libraries/achartengine/src/org/achartengine/chart/CubicLineChart.java
new file mode 100644 (file)
index 0000000..2011318
--- /dev/null
@@ -0,0 +1,120 @@
+/**\r
+ * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL\r
+ *  \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *  \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *  \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.achartengine.chart;\r
+\r
+import org.achartengine.model.Point;\r
+import org.achartengine.model.XYMultipleSeriesDataset;\r
+import org.achartengine.renderer.XYMultipleSeriesRenderer;\r
+\r
+import android.graphics.Canvas;\r
+import android.graphics.Paint;\r
+import android.graphics.Path;\r
+\r
+/**\r
+ * The interpolated (cubic) line chart rendering class.\r
+ */\r
+public class CubicLineChart extends LineChart {\r
+  /** The chart type. */\r
+  public static final String TYPE = "Cubic";\r
+\r
+  private float firstMultiplier;\r
+\r
+  private float secondMultiplier;\r
+\r
+  private Point p1 = new Point();\r
+\r
+  private Point p2 = new Point();\r
+\r
+  private Point p3 = new Point();\r
+\r
+  public CubicLineChart() {\r
+    // default is to have first control point at about 33% of the distance,\r
+    firstMultiplier = 0.33f;\r
+    // and the next at 66% of the distance.\r
+    secondMultiplier = 1 - firstMultiplier;\r
+  }\r
+\r
+  /**\r
+   * Builds a cubic line chart.\r
+   * \r
+   * @param dataset the dataset\r
+   * @param renderer the renderer\r
+   * @param smoothness smoothness determines how smooth the curve should be,\r
+   *          range [0->0.5] super smooth, 0.5, means that it might not get\r
+   *          close to control points if you have random data // less smooth,\r
+   *          (close to 0) means that it will most likely touch all control //\r
+   *          points\r
+   */\r
+  public CubicLineChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,\r
+      float smoothness) {\r
+    super(dataset, renderer);\r
+    firstMultiplier = smoothness;\r
+    secondMultiplier = 1 - firstMultiplier;\r
+  }\r
+\r
+  @Override\r
+  protected void drawPath(Canvas canvas, float[] points, Paint paint, boolean circular) {\r
+    Path p = new Path();\r
+    float x = points[0];\r
+    float y = points[1];\r
+    p.moveTo(x, y);\r
+\r
+    int length = points.length;\r
+    if (circular) {\r
+      length -= 4;\r
+    }\r
+\r
+    for (int i = 0; i < length; i += 2) {\r
+      int nextIndex = i + 2 < length ? i + 2 : i;\r
+      int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;\r
+      calc(points, p1, i, nextIndex, secondMultiplier);\r
+      p2.setX(points[nextIndex]);\r
+      p2.setY(points[nextIndex + 1]);\r
+      calc(points, p3, nextIndex, nextNextIndex, firstMultiplier);\r
+      // From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3\r
+      p.cubicTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());\r
+    }\r
+    if (circular) {\r
+      for (int i = length; i < length + 4; i += 2) {\r
+        p.lineTo(points[i], points[i + 1]);\r
+      }\r
+      p.lineTo(points[0], points[1]);\r
+    }\r
+    canvas.drawPath(p, paint);\r
+  }\r
+\r
+  private void calc(float[] points, Point result, int index1, int index2, final float multiplier) {\r
+    float p1x = points[index1];\r
+    float p1y = points[index1 + 1];\r
+    float p2x = points[index2];\r
+    float p2y = points[index2 + 1];\r
+\r
+    float diffX = p2x - p1x; // p2.x - p1.x;\r
+    float diffY = p2y - p1y; // p2.y - p1.y;\r
+    result.setX(p1x + (diffX * multiplier));\r
+    result.setY(p1y + (diffY * multiplier));\r
+  }\r
+\r
+  /**\r
+   * Returns the chart type identifier.\r
+   * \r
+   * @return the chart type\r
+   */\r
+  public String getChartType() {\r
+    return TYPE;\r
+  }\r
+\r
+}\r