Takes an array of 2D coordinates representing corners and returns the smallest rectangle containing those coordinates. - Android Graphics

Android examples for Graphics:Rectangle

Description

Takes an array of 2D coordinates representing corners and returns the smallest rectangle containing those coordinates.

Demo Code


//package com.java2s;
import android.graphics.RectF;

public class Main {
    /**//from  w ww. j a  v a 2 s.c o  m
     * Takes an array of 2D coordinates representing corners and returns the
     * smallest rectangle containing those coordinates.
     *
     * @param array array of 2D coordinates
     * @return smallest rectangle containing coordinates
     */
    public static RectF trapToRect(float[] array) {
        RectF r = new RectF(Float.POSITIVE_INFINITY,
                Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY,
                Float.NEGATIVE_INFINITY);
        for (int i = 1; i < array.length; i += 2) {
            float x = array[i - 1];
            float y = array[i];
            r.left = (x < r.left) ? x : r.left;
            r.top = (y < r.top) ? y : r.top;
            r.right = (x > r.right) ? x : r.right;
            r.bottom = (y > r.bottom) ? y : r.bottom;
        }
        r.sort();
        return r;
    }
}

Related Tutorials