Here you can find the source of slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)
Parameter | Description |
---|---|
p1 | the first point |
p2 | the second point |
double
with the slope, which may be infinite (if the points are spaced vertically) or NaN (if they're the same)
public static double slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)
//package com.java2s; /*//from www . j ava 2 s . co m * #%L * BlaiseMath * -- * Copyright (C) 2009 - 2015 Elisha Peterson * -- * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.awt.geom.Point2D; public class Main { /** * Computes slope between two points. * @param p1 the first point * @param p2 the second point * @return a <code>double</code> with the slope, * which may be infinite (if the points are spaced vertically) or NaN (if they're the same) */ public static double slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2) { if (p1.equals(p2)) { return Double.NaN; } else if (p1.x == p2.x) { return Double.POSITIVE_INFINITY; } else { return (p2.y - p1.y) / (p2.x - p1.x); } } }