hexagram/src/thermo.c

96 lines
2.5 KiB
C
Raw Normal View History

#include <math.h>
#include <hexagram/gauge.h>
#include <hexagram/thermo.h>
2019-06-09 15:46:41 -05:00
void hexagram_thermo_init(hexagram_thermo *thermo,
double x,
double y,
double radius,
double redline) {
hexagram_gauge_init(&thermo->gauge, x, y, radius,
300.0 * (M_PI / 180.0),
420.0 * (M_PI / 180.0));
thermo->redline = redline;
}
void hexagram_thermo_draw_face(hexagram_thermo *thermo,
cairo_t *cr) {
int i;
cairo_select_font_face(cr, "Helvetica",
2019-06-09 15:46:41 -05:00
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
2019-06-09 15:46:41 -05:00
cairo_set_font_size(cr, thermo->gauge.radius * 0.2);
cairo_set_source_rgb(cr, 1, 1, 1);
2019-06-09 15:46:41 -05:00
cairo_arc(cr,
thermo->gauge.x,
thermo->gauge.y,
thermo->gauge.radius,
0,
2.0 * M_PI);
cairo_stroke(cr);
/*
* Draw face numbers
*/
2019-06-09 15:46:41 -05:00
cairo_move_to(cr,
thermo->gauge.x - 0.8 * thermo->gauge.radius,
thermo->gauge.y - 0.1 * thermo->gauge.radius);
cairo_show_text(cr, "120");
2019-06-09 15:46:41 -05:00
cairo_move_to(cr,
thermo->gauge.x - 0.15 * thermo->gauge.radius,
thermo->gauge.y - 0.5 * thermo->gauge.radius);
cairo_show_text(cr, "190");
2019-06-09 15:46:41 -05:00
cairo_move_to(cr,
thermo->gauge.x + 0.5 * thermo->gauge.radius,
thermo->gauge.y - 0.1 * thermo->gauge.radius);
cairo_show_text(cr, "260");
/*
* Draw gauge graduations
*/
2019-06-28 18:10:01 -05:00
for (i=0; i<=16; i++) {
double min_radius = (i >= 14)?
(i % 2? 0.8: 0.7):
(i % 4? 0.8: 0.7);
if ((i * 8.23529411) + 120 >= thermo->redline) {
cairo_set_source_rgb(cr, 1, 0, 0);
}
2019-06-09 15:46:41 -05:00
hexagram_gauge_draw_mark(&thermo->gauge,
cr,
2019-06-28 18:10:01 -05:00
min_radius,
2019-06-09 15:46:41 -05:00
0.90,
2019-06-28 18:10:01 -05:00
i / 16.0);
}
}
2019-06-09 15:46:41 -05:00
void hexagram_thermo_draw_needle(hexagram_thermo *thermo,
cairo_t *cr,
double temp) {
2019-06-13 23:59:47 -05:00
double fahrenheit = temp * (9.0 / 5.0) + 32.0;
if (fahrenheit < 120) {
fahrenheit = 120;
} else if (fahrenheit > 260) {
fahrenheit = 260;
}
2019-06-09 15:46:41 -05:00
hexagram_gauge_draw_needle(&thermo->gauge,
cr,
0.9,
2019-06-13 23:59:47 -05:00
(fahrenheit - 120) / 140.0);
}