Here you can find the source of cubeIntersectsSphere(int x1, int y1, int z1, int x2, int y2, int z2, int sX, int sY, int sZ, int radius)
Parameter | Description |
---|---|
x1 | the cube's first point x |
y1 | the cube's first point y |
z1 | the cube's first point z |
x2 | the cube's second point x |
y2 | the cube's second point y |
z2 | the cube's second point z |
sX | the sphere's middle x |
sY | the sphere's middle y |
sZ | the sphere's middle z |
radius | the sphere's radius |
public static boolean cubeIntersectsSphere(int x1, int y1, int z1, int x2, int y2, int z2, int sX, int sY, int sZ, int radius)
//package com.java2s; /*/*from w w w . j a v a 2s.c o m*/ * This file is part of the L2J Mobius project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Checks if the cube intersects the sphere. * @param x1 the cube's first point x * @param y1 the cube's first point y * @param z1 the cube's first point z * @param x2 the cube's second point x * @param y2 the cube's second point y * @param z2 the cube's second point z * @param sX the sphere's middle x * @param sY the sphere's middle y * @param sZ the sphere's middle z * @param radius the sphere's radius * @return {@code true} if cube intersects sphere, {@code false} otherwise */ public static boolean cubeIntersectsSphere(int x1, int y1, int z1, int x2, int y2, int z2, int sX, int sY, int sZ, int radius) { double d = radius * radius; if (sX < x1) { d -= Math.pow(sX - x1, 2); } else if (sX > x2) { d -= Math.pow(sX - x2, 2); } if (sY < y1) { d -= Math.pow(sY - y1, 2); } else if (sY > y2) { d -= Math.pow(sY - y2, 2); } if (sZ < z1) { d -= Math.pow(sZ - z1, 2); } else if (sZ > z2) { d -= Math.pow(sZ - z2, 2); } return d > 0; } }