Java examples for java.lang:Math Curve
Checks whether the line and the cubic curve intersect.
public class Main{ /**/*w ww . j a va 2 s. c o m*/ * Checks whether the line (x1, y1) - (x2, y2) and the cubic curve (cx1, cy1) - (cx2, cy2) - * (cx3, cy3) - (cx4, cy4) intersect. The points of intersection are saved to {@code points}. * Therefore {@code points} must be of length at least 6. * * @return the numbers of roots that lie in the defined interval. */ public static int intersectLineAndCubic(float x1, float y1, float x2, float y2, float cx1, float cy1, float cx2, float cy2, float cx3, float cy3, float cx4, float cy4, float[] params) { float[] eqn = new float[4]; float[] t = new float[3]; float[] s = new float[3]; float dy = y2 - y1; float dx = x2 - x1; int quantity = 0; int count = 0; eqn[0] = (cy1 - y1) * dx + (x1 - cx1) * dy; eqn[1] = -3 * (cy1 - cy2) * dx + 3 * (cx1 - cx2) * dy; eqn[2] = (3 * cy1 - 6 * cy2 + 3 * cy3) * dx - (3 * cx1 - 6 * cx2 + 3 * cx3) * dy; eqn[3] = (-3 * cy1 + 3 * cy2 - 3 * cy3 + cy4) * dx + (3 * cx1 - 3 * cx2 + 3 * cx3 - cx4) * dy; if ((count = Crossing.solveCubic(eqn, t)) == 0) { return 0; } for (int i = 0; i < count; i++) { if (dx != 0) { s[i] = (cubic(t[i], cx1, cx2, cx3, cx4) - x1) / dx; } else if (dy != 0) { s[i] = (cubic(t[i], cy1, cy2, cy3, cy4) - y1) / dy; } else { s[i] = 0f; } if (t[i] >= 0 && t[i] <= 1 && s[i] >= 0 && s[i] <= 1) { params[2 * quantity] = t[i]; params[2 * quantity + 1] = s[i]; ++quantity; } } return quantity; } public static float cubic(float t, float x1, float x2, float x3, float x4) { return x1 * (1f - t) * (1f - t) * (1f - t) + 3f * x2 * (1f - t) * (1f - t) * t + 3f * x3 * (1f - t) * t * t + x4 * t * t * t; } }