Permutation matrices research.
at master 2.2 kB view raw
1#include "fractal.hpp" 2#include <cmath> 3#include <array> 4#include "prelude.hpp" 5 6 7fn distance_sq(Size x1, Size y1, Size x2, Size y2) -> F64 { 8 let rise = y2 - y1; 9 let run = x2 - x1; 10 return rise * rise + run * run; 11} 12 13fn distance(Size x1, Size y1, Size x2, Size y2) -> F64 { 14 return sqrt(distance_sq(x1, y1, x2, y2)); 15} 16 17RGB::RGB(U8 red, U8 green, U8 blue) : Red(red) , Green(green), Blue(blue) 18{ 19} 20 21fn RGB::to_hsv() const -> Color { 22 let r = Red / 255.0; 23 let g = Green / 255.0; 24 let b = Blue / 255.0; 25 let x_max = std::max(r, std::max(g, b)); 26 let v = x_max; 27 let x_min = std::min(r, std::min(g, b)); 28 let c = x_max - x_min; 29 let l = v - c / 2.0; 30 let h = v == r ? 60.0 * (I64((g - b) / c) % 6) : 31 v == g ? 60.0 * ((b - r) / c + 2) : 32 v == b ? 60.0 * ((r - g) / c + 4) : 0.0; 33 let s = v == 0 ? 0.0 : c / v; 34 35 return Color(h, s, v); 36} 37 38Color::Color() : hue(0.0), saturation(0.0), value(0.0) 39{ 40} 41 42Color::Color(F64 hue, F64 saturation, F64 value) : hue(hue) 43 , saturation(saturation), value(value) 44{ 45} 46 47Color::Color(U32 val) 48 : hue(0.0), saturation(0.0) 49{ 50 value = (double) val / 255.0; 51} 52 53fn Color::to_rgb() const -> RGB { 54 let chroma = saturation * value; 55 let cube_hue = hue / 60.0; 56 let tmp = chroma * (1.0 - fabs(fmod(cube_hue, 2.0) - 1.0)); 57 58 var components = std::array{ 0.0, 0.0, 0.0 }; 59 if (cube_hue < 1.0) 60 components = { chroma, tmp, 0.0 }; 61 else if (cube_hue < 2.0) 62 components = { tmp, chroma, 0.0 }; 63 else if(cube_hue < 3.0) 64 components = { 0.0, chroma, tmp }; 65 else if(cube_hue < 4.0) 66 components = { 0.0, tmp, chroma }; 67 else if(cube_hue < 5.0) 68 components = { tmp, 0.0, chroma }; 69 else 70 components = { chroma, 0.0, tmp }; 71 72 let match = value - chroma; 73 for (var& component : components) 74 component += match, component *= 255; 75 76 const auto c = RGB(round(components[0]), round(components[1]), round(components[2])); 77 return c; 78} 79 80fn Color::lerp(Ref<const Color> rhs, double t) const -> Color 81{ 82 return Color(std::lerp(hue, rhs.hue, t), std::lerp(saturation, rhs.saturation, t), std::lerp(value, rhs.value, t)); 83} 84 85Point::Point(F64 x, F64 y) 86 : x(x), y(y) 87{ 88}