atomes 1.1.15
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
draw.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2024 by CNRS and University of Strasbourg */
15
22/*
23* This file: 'draw.c'
24*
25* Contains:
26*
27
28- The function to draw a curve
29
30*
31*/
32
33#include <gtk/gtk.h>
34#include <gdk/gdk.h>
35#include <stdlib.h>
36#include <math.h>
37#include <cairo.h>
38
39#include "global.h"
40#include "curve.h"
41
85void draw_curve (cairo_t * cr,
86 int cid,
87 int rid,
88 project * this_proj,
89 int points,
90 ColRGBA withcolor,
91 int xscale,
92 int yscale,
93 int asp,
94 int vdash,
95 double thick,
96 int glyp,
97 double gize,
98 int freq,
99 double hwidth,
100 double hopac,
101 int hpos,
102 int extra,
103 int pid)
104{
105 int i, j, k;
106 double x, y;
107 double x1, x2, y1, y2;
108 double dx1, dx2, dy1, dy2;
109 double slope, bval;
110 double ** plotdata;
111 gboolean plot;
112 gboolean dglyp;
113 curve_dash * dasht;
114
115 plotdata = allocddouble (points, 2);
116 for ( i=0 ; i < points; i++ )
117 {
118 if (xscale == 0)
119 {
120 plotdata[i][0] = x_min + XDRAW * (this_proj -> curves[rid][cid] -> data[0][i] - cxy[0])/ xmax;
121 }
122 else
123 {
124 x = (i+1) * this_proj -> num_delta[rid] * this_proj -> delta[rid] * pow(10, dxlog);
125 x = log(x) / log(pow(10, xlog));
126 plotdata[i][0] = x_min + XDRAW * x;
127 }
128 if (yscale == 0)
129 {
130 plotdata[i][1] = y_min + YDRAW * (this_proj -> curves[rid][cid] -> data[1][i] - cxy[1]) / ymax;
131 }
132 else
133 {
134 y = this_proj -> curves[rid][cid] -> data[1][i] * pow(10, dylog);
135 y = log(y) / log(pow(10, ylog));
136 plotdata[i][1] = y_min + YDRAW * y;
137 }
138#ifdef DEBUG
139 // g_debug ("CURVE: DRAWCURVE: x= %f, y= %f", plotdata[i][0], plotdata[i][1]);
140#endif
141 }
142 if (vdash > 0)
143 {
144 dasht = selectdash (vdash);
145 cairo_set_dash(cr, dasht -> a, dasht -> b, 0.0);
146 cairo_set_line_width (cr, thick);
147 g_free (dasht);
148 }
149 else
150 {
151 cairo_set_line_width (cr, 0.0);
152 }
153 cairo_set_source_rgba (cr, withcolor.red,
154 withcolor.green,
155 withcolor.blue, 1.0);
156
157 if (asp == 0)
158 {
159 j = 0;
160 k = 1;
161 if (rid == RI) j = 2;
162 for ( i = j ; i < points - k ; i ++)
163 {
164 plot = TRUE;
165 dglyp = FALSE;
166 slope = (plotdata[i+k][1] - plotdata[i][1])/ (plotdata[i+k][0] - plotdata[i][0]);
167 bval = plotdata[i][1] - slope*plotdata[i][0];
168 dy1 = slope*x_min + bval;
169 dy2 = slope*x_max + bval;
170 if (slope < 0)
171 {
172 dx1 = (y_min - bval) / slope;
173 dx2 = (y_max - bval) / slope;
174 }
175 else
176 {
177 dx1 = (y_max - bval) / slope;
178 dx2 = (y_min - bval) / slope;
179 }
180 if (plotdata[i][0] < x_min && plotdata[i+k][0] < x_min)
181 {
182 plot = FALSE;
183 }
184 else if (plotdata[i][0] > x_max && plotdata[i+k][0] > x_max)
185 {
186 plot = FALSE;
187 }
188 if (plotdata[i][1] < y_max && plotdata[i+k][1] < y_max)
189 {
190 plot = FALSE;
191 }
192 else if (plotdata[i][1] > y_min && plotdata[i+k][1] > y_min)
193 {
194 plot = FALSE;
195 }
196 if (plot)
197 {
198 if (plotdata[i][0] >= x_min && plotdata[i][0] <= x_max)
199 {
200 if(plotdata[i][1] >= y_max && plotdata[i][1] <= y_min)
201 {
202 x1 = plotdata[i][0];
203 y1 = plotdata[i][1];
204 dglyp = TRUE;
205 }
206 else
207 {
208 x1 = dx1;
209 y1 = (slope < 0) ? y_min : y_max;
210 }
211 }
212 else
213 {
214 x1 = x_min;
215 y1 = dy1;
216 }
217 if (plotdata[i+k][0] >= x_min && plotdata[i+k][0] <= x_max)
218 {
219 if (plotdata[i+k][1] >= y_max && plotdata[i+k][1] <= y_min)
220 {
221 x2 = plotdata[i+k][0];
222 y2 = plotdata[i+k][1];
223 }
224 else
225 {
226 x2 = dx2;
227 y2 = (slope < 0) ? y_max : y_min;
228 }
229 }
230 else
231 {
232 x2 = x_max;
233 y2 = dy2;
234 }
235 cairo_move_to (cr, x1, y1);
236 cairo_line_to (cr, x2, y2);
237 cairo_stroke (cr);
238 if (i % freq == 0 && dglyp) draw_glyph (cr, glyp, x1, y1, withcolor, gize);
239 if (i == points - 2 && (i+k) % freq == 0 && dglyp)
240 {
241 draw_glyph (cr, glyp, x2, y2, withcolor, gize);
242 }
243 }
244 }
245 }
246 else if (asp == 1)
247 {
248 j = 0;
249 k = 1;
250 if (rid == RI) j = 2;
251 // g_debug ("x_min= %f, x_max= %f, y_min= %f, y_max= %f", x_min, x_max, y_min, y_max);
252 for ( i = j ; i < points ; i ++)
253 {
254 if (this_proj -> curves[rid][cid] -> data[1][i] != 0.0)
255 {
256 if (plotdata[i][0] >= x_min && plotdata[i][0] <= x_max)
257 {
258 if (plotdata[i][1] <= y_min || (cxy[1] <= 0.0 && plotdata[i][1] > y_min))
259 {
260 x1 = plotdata[i][0] - hwidth * 0.5 * XDRAW / xmax;
261 if (pid) x1 -= hwidth * 0.5 * ((float)pid / (float)extra) * XDRAW / xmax;
262 x2 = hwidth * XDRAW / xmax;
263 if (x1 < x_min)
264 {
265 x2 -= (x_min - x1);
266 x1 = x_min;
267 }
268 if (x1+x2 > x_max)
269 {
270 x2 = x_max - x1;
271 }
272 y1 = plotdata[i][1];
273 if (plotdata[i][1] > y_min)
274 {
275 y1 = y_min;
276 }
277 else if (plotdata[i][1] < y_max)
278 {
279 y1 = y_max;
280 }
281 y2 = y_min - y1;
282 if (cxy[1] <= 0.0)
283 {
284 y2 = y2 - cxy[1] * YDRAW / ymax;
285 }
286 if (hpos)
287 {
288 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
289 cairo_rectangle (cr, x1, y1, x2, y2);
290 cairo_fill(cr);
291 }
292 cairo_set_source_rgba (cr, withcolor.red,
293 withcolor.green,
294 withcolor.blue, hopac);
295 cairo_rectangle (cr, x1, y1, x2, y2);
296 cairo_fill(cr);
297 cairo_set_source_rgba (cr, withcolor.red,
298 withcolor.green,
299 withcolor.blue, 1.0);
300 y2 = y_min;
301 if (cxy[1] <= 0.0)
302 {
303 y2 = y2 - cxy[1] * YDRAW / ymax;
304 }
305 cairo_move_to (cr, x1, y2);
306 cairo_line_to (cr, x1, y1);
307 cairo_move_to (cr, x1, y1);
308 x2 = plotdata[i][0] + hwidth * 0.5 * XDRAW / xmax;
309 if (pid) x2 -= hwidth * 0.5 * ((float)pid / (float)extra) * XDRAW / xmax;
310 if (x2 > x_max)
311 {
312 x2 = x_max;
313 cairo_line_to(cr, x2, y1);
314 cairo_move_to(cr, x2, y1);
315 }
316 else
317 {
318 cairo_line_to(cr, x2, y1);
319 cairo_move_to(cr, x2, y1);
320 cairo_line_to(cr, x2, y2);
321 }
322 cairo_stroke(cr);
323 }
324 }
325 }
326 }
327 }
328 cairo_stroke(cr);
329 g_free (plotdata);
330}
int xlog
Definition curve.c:74
double cxy[2]
Definition curve.c:71
double xmax
Definition curve.c:67
int ylog
Definition curve.c:74
int dylog
Definition curve.c:75
double YDRAW
Definition curve.c:65
curve_dash * selectdash(int iddash)
setup dash pointer
Definition curve.c:134
double XDRAW
Definition curve.c:65
double y_max
Definition curve.c:69
double x_max
Definition curve.c:68
double y_min
Definition curve.c:69
int dxlog
Definition curve.c:75
double x_min
Definition curve.c:68
double ymax
Definition curve.c:67
Variable declarations for the curve widget Functions for interactions with the curve widget.
void draw_glyph(cairo_t *in, int theglyph, double x, double y, ColRGBA gcolor, double size)
draw glyph at (x,y)
Definition glyph.c:88
float extra
void draw_curve(cairo_t *cr, int cid, int rid, project *this_proj, int points, ColRGBA withcolor, int xscale, int yscale, int asp, int vdash, double thick, int glyp, double gize, int freq, double hwidth, double hopac, int hpos, int extra, int pid)
draw target curve to the cairo drawing context
Definition draw.c:85
double ** allocddouble(int xal, int yal)
allocate a double ** pointer
Definition global.c:475
Global variable declarations Global convenience function declarations Global data structure defin...
#define RI
Definition global.h:328
image * plot
Definition ogl_draw.c:66
double y
Definition ogl_draw.c:57
double x
Definition ogl_draw.c:57
float blue
Definition global.h:124
float red
Definition global.h:122
float green
Definition global.h:123
int b
Definition tab-1.c:95
int a
Definition tab-1.c:95