Here you can find the source of squareAtan2(float y, float x)
Parameter | Description |
---|---|
y | The y coordinate of the given direction. |
x | The x coordinate of the given direction. |
public static float squareAtan2(float y, float x)
//package com.java2s; /*/*ww w. j a v a 2s. co m*/ * Copyright (c) 2015 Sam Johnson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ public class Main { /** * Calculates the square angle corresponding with the given direction. * * @param y The y coordinate of the given direction. * @param x The x coordinate of the given direction. * @return The square angle corresponding with the given direction. */ public static float squareAtan2(float y, float x) { float squareLength = Math.max(Math.abs(x), Math.abs(y)); y /= squareLength; x /= squareLength; int signs = (x < 0.0f ? 1 : 0) | (y < 0.0f ? 2 : 0); switch (signs) { case 0: return -x + y + 1.0f; //(+, +) case 1: return -x - y + 3.0f; //(-, +) case 2: return x + y + 7.0f; //(+, -) case 3: return x - y + 5.0f; //(-, -) default: assert false : signs; //Shouldn't happen. return Float.NaN; } } /** * Returns the greater of the two given values. Does not check for negative * zero or NaN. * * @param x0 Any float. * @param x1 Any float. * @return The greater of the two given values. */ public static float max(float x0, float x1) { return x0 >= x1 ? x0 : x1; } }