1 /* 2 * Copyright 2012 AndroidPlot.com 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.androidplot.util; 18 19 import android.graphics.PointF; 20 import android.graphics.RectF; 21 22 /** 23 * Utility methods for converting pixel coordinates into real values and vice versa. 24 */ 25 public class ValPixConverter { 26 private static final int ZERO = 0; 27 28 29 public static float valToPix(double val, double min, double max, float lengthPix, boolean flip) { 30 if(lengthPix <= ZERO) { 31 throw new IllegalArgumentException("Length in pixels must be greater than 0."); 32 } 33 double range = range(min, max); 34 double scale = lengthPix / range; 35 double raw = val - min; 36 float pix = (float)(raw * scale); 37 38 if(flip) { 39 pix = (lengthPix - pix); 40 } 41 return pix; 42 } 43 44 public static double range(double min, double max) { 45 return (max-min); 46 } 47 48 49 public static double valPerPix(double min, double max, float lengthPix) { 50 double valRange = range(min, max); 51 return valRange/lengthPix; 52 } 53 54 /** 55 * Convert a value in pixels to the type passed into min/max 56 * @param pix 57 * @param min 58 * @param max 59 * @param lengthPix 60 * @param flip True if the axis should be reversed before calculated. This is the case 61 * with the y axis for screen coords. 62 * @return 63 */ 64 public static double pixToVal(float pix, double min, double max, float lengthPix, boolean flip) { 65 if(pix < ZERO) { 66 throw new IllegalArgumentException("pixel values cannot be negative."); 67 } 68 69 if(lengthPix <= ZERO) { 70 throw new IllegalArgumentException("Length in pixels must be greater than 0."); 71 } 72 float pMult = pix; 73 if(flip) { 74 pMult = lengthPix - pix; 75 } 76 double range = range(min, max); 77 return ((range / lengthPix) * pMult) + min; 78 } 79 80 /** 81 * Converts a real value into a pixel value. 82 * @param x Real d (domain) component of the point to convert. 83 * @param y Real y (range) component of the point to convert. 84 * @param plotArea 85 * @param minX Minimum visible real value on the d (domain) axis. 86 * @param maxX Maximum visible real value on the y (domain) axis. 87 * @param minY Minimum visible real value on the y (range) axis. 88 * @param maxY Maximum visible real value on the y (range axis. 89 * @return 90 */ 91 public static PointF valToPix(Number x, Number y, RectF plotArea, Number minX, Number maxX, Number minY, Number maxY) { 92 float pixX = ValPixConverter.valToPix(x.doubleValue(), minX.doubleValue(), maxX.doubleValue(), plotArea.width(), false) + (plotArea.left); 93 float pixY = ValPixConverter.valToPix(y.doubleValue(), minY.doubleValue(), maxY.doubleValue(), plotArea.height(), true) + plotArea.top; 94 return new PointF(pixX, pixY); 95 } 96 } 97