computerscare-vcv-modules

ComputerScare modules for VCV Rack
Log | Files | Refs

drawFunctions.hpp (4956B)


      1 #pragma once
      2 
      3 
      4 struct DrawHelper {
      5 
      6   NVGcontext* vg;
      7   DrawHelper(NVGcontext* ctx) {
      8     vg = ctx;
      9   }
     10   void drawShape(std::vector<Vec> points, NVGcolor fillColor = COLOR_COMPUTERSCARE_PINK) {
     11     drawShape(points, fillColor, COLOR_COMPUTERSCARE_TRANSPARENT, 0.f);
     12   }
     13   void drawShape(std::vector<Vec> points, NVGcolor fillColor, NVGcolor strokeColor) {
     14     drawShape(points, fillColor, strokeColor, 1.f);
     15   }
     16   void drawShape(std::vector<Vec> points, NVGcolor strokeColor, float thickness) {
     17     drawShape(points, COLOR_COMPUTERSCARE_TRANSPARENT, strokeColor, thickness);
     18   }
     19   void drawShape(std::vector<Vec> points, NVGcolor fillColor, NVGcolor strokeColor, float thickness) {
     20     unsigned int n = points.size();
     21     nvgSave(vg);
     22     nvgBeginPath(vg);
     23     nvgStrokeColor(vg, strokeColor);
     24     nvgStrokeWidth(vg, thickness);
     25     nvgFillColor(vg, fillColor);
     26     nvgMoveTo(vg, points[0].x, points[0].y);
     27 
     28     for (unsigned int i = 1; i < n; i++) {
     29       nvgLineTo(vg, points[i].x, points[i].y);
     30     }
     31 
     32     nvgClosePath(vg);
     33     nvgFill(vg);
     34     if (thickness > 0) {
     35       nvgStroke(vg);
     36     }
     37     nvgRestore(vg);
     38   }
     39   void drawDots(std::vector<Vec> points, NVGcolor fillColor = BLACK, float thickness = 1.f) {
     40     unsigned int n = points.size();
     41     //nvgSave(vg);
     42     // nvgBeginPath(vg);
     43     nvgBeginPath(vg);
     44     //nvgStrokeColor(vg, fillColor);
     45     //nvgStrokeWidth(vg, thickness);
     46     nvgFillColor(vg, fillColor);
     47 
     48     //nvgMoveTo(vg, 0,0);
     49     for (unsigned int i = 0; i < n; i++) {
     50       nvgCircle(vg, points[i].x, points[i].y, thickness);
     51     }
     52 
     53     nvgClosePath(vg);
     54     nvgFill(vg);
     55     /*if (thickness > 0) {
     56       nvgStroke(vg);
     57     }*/
     58     //nvgStroke(vg);
     59     // nvgRestore(vg);
     60   }
     61   //void drawLines(std::vector<Vec> points, float length=)
     62   void drawLines(std::vector<Vec> points, float length = 20, float angle = 0.f, float thickness = 5.f) {
     63     std::vector<Vec> angles;
     64     for (unsigned int i = 0; i < points.size(); i++) {
     65       angles.push_back(Vec(length, angle));
     66     }
     67     drawLines(points, angles, COLOR_COMPUTERSCARE_RED, thickness);
     68   }
     69   void drawLines(std::vector<Vec> points, std::vector<Vec> rTheta, std::vector<NVGcolor> strokeColors, std::vector<Vec> thicknesses) {
     70     unsigned int n = points.size();
     71 
     72     // nvgBeginPath(vg);
     73 
     74 
     75     //nvgMoveTo(vg, 0,0);
     76     for (unsigned int i = 0; i < n; i++) {
     77       nvgSave(vg);
     78       nvgBeginPath(vg);
     79       nvgStrokeWidth(vg, thicknesses[i].x);
     80       nvgStrokeColor(vg, strokeColors[i]);
     81       nvgMoveTo(vg, points[i].x, points[i].y);
     82       nvgLineTo(vg, points[i].x + rTheta[i].x * cos(rTheta[i].y), points[i].y + rTheta[i].x * sin(rTheta[i].y));
     83       nvgStroke(vg);
     84       nvgRestore(vg);
     85       //nvgClosePath(vg);
     86     }
     87 
     88 
     89   }
     90   void drawLines(std::vector<Vec> points, std::vector<Vec> rTheta, NVGcolor strokeColor = COLOR_COMPUTERSCARE_RED, float thickness = 1.f) {
     91     unsigned int n = points.size();
     92     nvgSave(vg);
     93     // nvgBeginPath(vg);
     94     nvgBeginPath(vg);
     95     nvgStrokeColor(vg, strokeColor);
     96     nvgStrokeWidth(vg, thickness);
     97 
     98     //nvgMoveTo(vg, 0,0);
     99     for (unsigned int i = 0; i < n; i++) {
    100       nvgMoveTo(vg, points[i].x, points[i].y);
    101       nvgLineTo(vg, points[i].x + rTheta[i].x * cos(rTheta[i].y), points[i].y + rTheta[i].x * sin(rTheta[i].y));
    102       //nvgClosePath(vg);
    103     }
    104     nvgStroke(vg);
    105     nvgRestore(vg);
    106   }
    107   void drawLines(int n, float dThickness, float dTheta, float dColor = 0.1) {
    108     nvgSave(vg);
    109     // nvgBeginPath(vg);
    110 
    111     //nvgMoveTo(vg, 0,0);
    112     float radius = 100000;
    113     float thickness = 10;
    114     for (int i = 0; i < n; i++) {
    115 
    116       NVGcolor color = sincolor(dColor * i / n);
    117       float angle = dTheta * i / n * 2 * M_PI;
    118       nvgBeginPath(vg);
    119       nvgStrokeColor(vg, color);
    120       nvgStrokeWidth(vg, thickness);
    121 
    122       float x0 = radius * cosf(angle);
    123       float y0 = radius * sinf(angle);
    124       float x1 = radius * cosf(angle + M_PI);
    125       float y1 = radius * sinf(angle + M_PI);
    126 
    127       nvgMoveTo(vg, x0, y0);
    128       nvgLineTo(vg, x1, y1);
    129       nvgClosePath(vg);
    130       nvgStroke(vg);
    131 
    132       thickness *= expf(dThickness);
    133 
    134     }
    135     nvgRestore(vg);
    136   }
    137   void drawField(std::vector<Vec> points, NVGcolor strokeColor = BLACK, float length = 4, float thickness = 1.f) {
    138     unsigned int n = points.size();
    139     nvgSave(vg);
    140     // nvgBeginPath(vg);
    141     nvgBeginPath(vg);
    142     nvgStrokeColor(vg, strokeColor);
    143     nvgStrokeWidth(vg, thickness);
    144 
    145     //nvgMoveTo(vg, 0,0);
    146     for (unsigned int i = 0; i < n; i++) {
    147       nvgMoveTo(vg, points[i].x, points[i].y);
    148       Vec end = points[i].plus(Vec(points[i].y, -points[i].x).normalize().mult(length));
    149       nvgLineTo(vg, end.x, end.y);
    150       //nvgClosePath(vg);
    151     }
    152     nvgStroke(vg);
    153     nvgRestore(vg);
    154   }
    155   NVGcolor sincolor(float t, std::vector<float> omega = {1, 2, 3}, std::vector<float> phi = {0.f, 5.f, 9.f}) {
    156     return nvgRGB(127 * (1 + sin(t * omega[0] + phi[0])), 127 * (1 + sin(t * omega[1] + phi[1])), 127 * (1 + sin(t * omega[2] + phi[2])));
    157   }
    158 
    159 };