source: trunk/SRC/Utilities/linearequation.pro @ 508

Last change on this file since 508 was 508, checked in by smasson, 7 years ago

minor improvments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1;+
2;
3; @file_comments
4; Calculate a linear equation of the type ax+by+c=0
5; thanks to coordinates of 2 points.
6; comment: we can have a table with pairs of points.
7;
8; @categories
9; Utilities
10;
11; @param POINT1 {in}{required}
12; This is the first point of (the) straight line(s) whose we want to calculate
13; equation(s)
14;
15; @param POINT2 {in}{required}
16; This is the second point of (the) straight line(s) whose we want to calculate
17; equation(s)
18;
19; There is 2 possibilities:
20;      1) point is a complex or a table of complex, where each element is the coordinates of the point.
21;      2) point is a table of real of dimension 2,number_of_straight_line.
22;         For each row of the table, we have coordinates of the point.
23;
24; @returns
25; abc is a table of dimension 3, number_of_straight_line,
26; where for each line of the table we obtain the 3 parameters
27; a, b and c of the linear equation ax+by+c=0
28;
29; @examples
30;
31;   IDL> abc=linearequation(complex(1,2),[3,4])
32;   IDL> print, abc[0]*1+abc[1]*2+abc[2]
33; 0.00000
34;
35; @history
36; Sebastien Masson (smasson\@lodyc.jussieu.fr)
37;          10 juin 2000
38;
39; @version
40; $Id$
41;
42;-
43FUNCTION linearequation, point1, point2
44;
45  compile_opt idl2, strictarrsubs
46;
47
48  if size(point1, /type) EQ 6 OR size(point1, /type) EQ 9 then begin
49    x1 = real_part(point1)
50    y1 = imaginary(point1)
51  ENDIF ELSE BEGIN
52    if size(point1, /type) EQ 5 then begin
53      x1 = double(reform(point1[0, *]))
54      y1 = double(reform(point1[1, *]))
55    ENDIF ELSE BEGIN
56      x1 = float(reform(point1[0, *]))
57      y1 = float(reform(point1[1, *]))
58    ENDELSE
59  ENDELSE
60 
61  if size(point2, /type) EQ 6 OR size(point2, /type) EQ 9 then begin
62    x2 = real_part(point2)
63    y2 = imaginary(point2)
64  ENDIF ELSE BEGIN
65    if size(point2, /type) EQ 5 then begin
66      x2 = double(reform(point2[0, *]))
67      y2 = double(reform(point2[1, *]))
68    ENDIF ELSE BEGIN
69      x2 = float(reform(point2[0, *]))
70      y2 = float(reform(point2[1, *]))
71    ENDELSE
72  ENDELSE
73 
74   vertical = where(x1 EQ x2)
75   novertical = where(x1 NE x2)
76   abc = fltarr(3, n_elements(x1))
77
78   IF novertical[0] NE -1 then BEGIN
79; y=mx+p
80      nele = n_elements(novertical)
81      m = (y2[novertical]-y1[novertical])/(x2[novertical]-x1[novertical])
82      p = (x2[novertical]*y1[novertical]-y2[novertical]*x1[novertical])/(x2[novertical]-x1[novertical])
83      abc[*, novertical] = [reform(-m, 1, nele), replicate(1, 1, nele), reform(-p, 1, nele)]
84   ENDIF
85   IF vertical[0] NE -1 then BEGIN
86; x=ny+p
87      nele = n_elements(vertical)
88      n = (x2[vertical]-x1[vertical])/(y2[vertical]-y1[vertical])
89      p = (y2[vertical]*x1[vertical]-x2[vertical]*y1[vertical])/(y2[vertical]-y1[vertical])
90      abc[*, vertical] = [replicate(1, 1, nele), reform(-n, 1, nele), reform(-p, 1, nele)]
91   ENDIF
92
93   return, abc
94end
Note: See TracBrowser for help on using the repository browser.