John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 1 | import cairo |
| 2 | from color import Color, palette |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 3 | import numpy as np |
| 4 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 5 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 6 | def set_color(cr, color, a=1): |
| 7 | if color.a == 1.0: |
| 8 | cr.set_source_rgba(color.r, color.g, color.b, a) |
| 9 | else: |
| 10 | cr.set_source_rgba(color.r, color.g, color.b, color.a) |
| 11 | |
| 12 | |
| 13 | def draw_px_cross(cr, x, y, length_px, color=palette["RED"]): |
| 14 | """Draws a cross with fixed dimensions in pixel space.""" |
| 15 | set_color(cr, color) |
| 16 | cr.move_to(x, y - length_px) |
| 17 | cr.line_to(x, y + length_px) |
| 18 | cr.stroke() |
| 19 | |
| 20 | cr.move_to(x - length_px, y) |
| 21 | cr.line_to(x + length_px, y) |
| 22 | cr.stroke() |
| 23 | set_color(cr, palette["WHITE"]) |
| 24 | |
| 25 | |
| 26 | def draw_px_x(cr, x, y, length_px1, color=palette["BLACK"]): |
| 27 | """Draws a x with fixed dimensions in pixel space.""" |
| 28 | length_px = length_px1 / np.sqrt(2) |
| 29 | set_color(cr, color) |
| 30 | cr.move_to(x - length_px, y - length_px) |
| 31 | cr.line_to(x + length_px, y + length_px) |
| 32 | cr.stroke() |
| 33 | |
| 34 | cr.move_to(x - length_px, y + length_px) |
| 35 | cr.line_to(x + length_px, y - length_px) |
| 36 | cr.stroke() |
| 37 | set_color(cr, palette["WHITE"]) |
| 38 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 39 | |
| 40 | def draw_circle(cr, x, y, radius, color=palette["RED"]): |
John Park | cf54516 | 2020-02-23 20:07:25 -0800 | [diff] [blame] | 41 | set_color(cr, color) |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 42 | cr.arc(x, y, radius, 0, 2 * np.pi) |
John Park | cf54516 | 2020-02-23 20:07:25 -0800 | [diff] [blame] | 43 | cr.fill() |
| 44 | cr.stroke() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 45 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 46 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 47 | def draw_control_points(cr, points, width=10, radius=4, color=palette["BLUE"]): |
| 48 | for i in range(0, len(points)): |
| 49 | draw_px_x(cr, points[i][0], points[i][1], width, color) |
| 50 | set_color(cr, color) |
| 51 | cr.arc(points[i][0], points[i][1], radius, 0, 2.0 * np.pi) |
| 52 | cr.fill() |
| 53 | set_color(cr, palette["WHITE"]) |
| 54 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 55 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 56 | def display_text(cr, text, widtha, heighta, widthb, heightb): |
| 57 | cr.scale(widtha, -heighta) |
| 58 | cr.show_text(text) |
| 59 | cr.scale(widthb, -heightb) |
| 60 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 61 | def draw_points(cr, p, size): |
| 62 | for i in range(0, len(p)): |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 63 | draw_px_cross(cr, p[i][0], p[i][1], size, Color( |
| 64 | 0, np.sqrt(0.2 * i), 0)) |