File indexing completed on 2024-05-12 15:27:08

0001 /***************************************************************************
0002     File                 : nsl_geom.c
0003     Project              : LabPlot
0004     Description          : NSL geometry functions
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2016 by Stefan Gerlach (stefan.gerlach@uni.kn)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "nsl_geom.h"
0030 #include <gsl/gsl_math.h>
0031 
0032 double nsl_geom_point_point_dist(double x1, double y1, double x2, double y2) {
0033     return gsl_hypot(x2-x1, y2-y1);
0034 }
0035 
0036 double nsl_geom_point_line_dist(double x1, double y1, double x2, double y2, double xp, double yp) {
0037     return fabs( (xp-x1)*(y2-y1) - (x2-x1)*(yp-y1) ) / nsl_geom_point_point_dist(x1, y1, x2, y2);
0038 }
0039 
0040 double nsl_geom_point_line_dist_y(double x1, double y1, double x2, double y2, double xp, double yp) {
0041     return fabs( yp - y1 - (y2-y1)*(xp-x1)/(x2-x1) );
0042 }
0043 
0044 double nsl_geom_three_point_area(double x1, double y1, double x2, double y2, double x3, double y3) {
0045     return fabs( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) ) / 2.;
0046 }
0047 
0048 double nsl_geom_point_point_dist3(double x1, double y1, double z1, double x2, double y2, double z2) {
0049     return gsl_hypot3(x2-x1, y2-y1, z2-z1);
0050 }