lib/zen/src/diagram/space.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const category = @import("category.zig");
 2 const domain = @import("domain.zig");
 3 const model = @import("model.zig");
 4 const root = @import("root.zig");
 5 
 6 const spec = root.spec;
 7 const Bounds = model.Bounds;
 8 
 9 pub fn xCoordinate(value: spec.XValue, bounds: Bounds, left: f64, right: f64) !f64 {
10     if (bounds.categorical) {
11         var buffer: [category.label_buffer_bytes]u8 = undefined;
12         const label_value = category.label(&buffer, value);
13         return category.coordinate(label_value, bounds, left, right);
14     }
15     return xNumberCoordinate(value.number, bounds, left, right);
16 }
17 
18 pub fn xNumberCoordinate(value: f64, bounds: Bounds, left: f64, right: f64) !f64 {
19     return left + try domain.ratio(value, bounds.x_min, bounds.x_max, bounds.x_kind, bounds.x_base) * (right - left);
20 }
21 
22 pub fn yCoordinate(value: f64, bounds: Bounds, bottom: f64, top: f64) !f64 {
23     return bottom - try domain.ratio(value, bounds.y_min, bounds.y_max, bounds.y_kind, bounds.y_base) * (bottom - top);
24 }
25 
26 pub fn xDistance(value: f64, bounds: Bounds, left: f64, right: f64) f64 {
27     return value / (bounds.x_max - bounds.x_min) * (right - left);
28 }
29 
30 pub fn yDistance(value: f64, bounds: Bounds, bottom: f64, top: f64) f64 {
31     return value / (bounds.y_max - bounds.y_min) * (bottom - top);
32 }