Android examples for java.lang:Math Geometry
Checks if two lines are parallel
/******************************************************************************* * Copyright (c) 2011 MadRobot./*www . j a va 2 s. c om*/ * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ //package com.java2s; import android.graphics.PointF; public class Main { /** * Defines the distance below which two points are considered to coincide. */ public static final double VERY_SMALL_DISTANCE = 0.000001; /** * Checks if two lines are parallel * * @param line1 * he first line * @param line2 * the second line * @return returns true if the lines are parallel, false otherwise */ public static boolean areParallel(PointF line1Start, PointF line1End, PointF line2Start, PointF line2End) { // If both lines are vertical, they are parallel if (isVertical(line1Start, line1End) && isVertical(line2Start, line2End)) { return true; } else // If one of them is vertical, they are not parallel if (isVertical(line1Start, line1End) || isVertical(line2Start, line2End)) { return false; } else // General case. If their slopes are the same, they are parallel { return (Math.abs(getSlope(line1Start, line1End) - getSlope(line2Start, line2End)) < VERY_SMALL_DISTANCE); } } /** * Checks if a line is vertical. * * @param line * the line to check * @return returns true if the line is vertical, false otherwise. */ public static boolean isVertical(PointF lineStart, PointF lineEnd) { return ((Math.abs(lineStart.x - lineEnd.x) < VERY_SMALL_DISTANCE)); } /** * Return the slope of a line * * @param lineStart * @param lineEnd * @return */ public static double getSlope(PointF lineStart, PointF lineEnd) { return (((lineStart.y) - lineEnd.y) / ((lineStart.x) - lineEnd.x)); } }