2 * $Id: compact.c,v 1.6 2004/07/01 17:10:12 broeker Exp $
6 /* GNUPLOT - compact.c */
8 /* contains routines to compress a vector stream without modifying it */
11 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
13 * Permission to use, copy, and distribute this software and its
14 * documentation for any purpose with or without fee is hereby granted,
15 * provided that the above copyright notice appear in all copies and
16 * that both that copyright notice and this permission notice appear
17 * in supporting documentation.
19 * Permission to modify the software is granted, but not the right to
20 * distribute the complete modified source code. Modifications are to
21 * be distributed as patches to the released version. Permission to
22 * distribute binaries produced by compiling modified sources is granted,
24 * 1. distribute the corresponding source modifications from the
25 * released version in the form of a patch file along with the binaries,
26 * 2. add special version identification to distinguish your version
27 * in addition to the base release version number,
28 * 3. provide your name and address as the primary contact for the
29 * support of your modified version, and
30 * 4. retain our contact information in regard to use of the base
32 * Permission to distribute the released version of the source code along
33 * with corresponding source modifications in the form of a patch file is
34 * granted with same provisions 2 through 4 for binary distributions.
36 * This software is provided "as is" without express or implied warranty
37 * to the extent permitted by applicable law.
42 static int compact_slope __PROTO((int xp[], int yp[], int isa_move[], int *sz, double delta));
44 /* replaces runs of constant slope in the buffer with single vectors
45 returns the number of points eliminated */
47 compact_slope(int xp[], int yp[], int isa_move[], int *sz, double delta)
49 int dx, dy, old_size, new_index, i, start;
50 float slope, old_slope;
56 old_slope = (float) (yp[1] - yp[0]) / (float) (xp[1] - xp[0]);
58 old_slope = (float) (yp[1] - yp[0]) / (float) (0.00001 + xp[1] - xp[0]);
60 for (i = 2; i < old_size; i++) {
61 dx = xp[i] - xp[i - 1];
62 dy = yp[i] - yp[i - 1];
64 slope = (float) dy / (float) dx;
66 slope = (float) dy / ((float) dx + 0.00001);
67 if ((ABS(slope - old_slope) > delta) || (isa_move[i])) {
68 xp[new_index] = xp[start];
69 yp[new_index] = yp[start];
70 isa_move[new_index] = isa_move[start];
73 xp[new_index] = xp[i - 1];
74 yp[new_index] = yp[i - 1];
75 isa_move[new_index] = isa_move[i - 1];
79 /* this is the slope for the new run */
83 /* copy the last point into the new array */
84 xp[new_index] = xp[old_size - 1];
85 yp[new_index] = yp[old_size - 1];
86 isa_move[new_index] = isa_move[old_size - 1];
89 return (old_size - *sz);
93 # if 0 /* currently unused */
94 static int compact_int __PROTO((int xp[], int yp[], int isa_move[], int *size));
96 /* compacts the vector list by compressing runs of constant
98 use this if floating point is too expensive!
99 more naive than compact_slope; doesn't compact as much as possible
100 returns the number of points eliminated */
102 compact_int(int xp[], int yp[], int isa_move[], int *size)
104 int dx, dy, old_dx, old_dy, start, index, i, old_size;
107 old_dx = xp[1] - xp[0];
108 old_dy = yp[1] - yp[0];
109 for (i = 2; i < *size; i++) {
110 dx = xp[i] - xp[i - 1];
111 dy = yp[i] - yp[i - 1];
112 if ((ABS(dx - old_dx) + ABS(dy - old_dy) != 0) || (isa_move[i])) {
113 /* we've reached the end of a run */
114 xp[index] = xp[start];
115 yp[index] = yp[start];
116 isa_move[index] = isa_move[start];
118 if (start != i - 1) {
119 xp[index] = xp[i - 1];
120 yp[index] = yp[i - 1];
121 isa_move[index] = isa_move[i - 1];
129 /* include the last point */
130 xp[index] = xp[*size - 1];
131 yp[index] = yp[*size - 1];
132 isa_move[index] = isa_move[*size - 1];
136 return (old_size - *size);
140 #endif /* !defined(COMPACT) */