Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import android.graphics.PointF;

public class Main {
    public static final int CPT_RED = 0;
    public static final int CPT_GREEN = 1;
    public static final int CPT_BLUE = 2;

    /**
     * Method to see if the given XY value is within the reach of the lamps.
     * 
     * @param p the point containing the X,Y value
     * @return true if within reach, false otherwise.
     */
    private static boolean checkPointInLampsReach(PointF p, ArrayList<PointF> colorPoints) {
        PointF red = colorPoints.get(CPT_RED);
        PointF green = colorPoints.get(CPT_GREEN);
        PointF blue = colorPoints.get(CPT_BLUE);
        PointF v1 = new PointF(green.x - red.x, green.y - red.y);
        PointF v2 = new PointF(blue.x - red.x, blue.y - red.y);
        PointF q = new PointF(p.x - red.x, p.y - red.y);
        float s = crossProduct(q, v2) / crossProduct(v1, v2);
        float t = crossProduct(v1, q) / crossProduct(v1, v2);
        if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f)) {
            return true;
        } else {
            return false;

        }
    }

    /**
     * Calculates crossProduct of two 2D vectors / points.
     * 
     * @param p1 first point used as vector
     * @param p2 second point used as vector
     * @return crossProduct of vectors
     */
    private static float crossProduct(PointF p1, PointF p2) {

        return (p1.x * p2.y - p1.y * p2.x);
    }
}