Last active
January 11, 2017 20:23
-
-
Save abstractalgo/fe1fcb52fa579ec42a9c6ba2039dbcd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <vector> | |
struct pt | |
{ | |
double x; | |
double y; | |
pt(double a=0,double b=0) : x(a), y(b) {} | |
void mul(double s) { | |
x *= s; | |
y *= s; | |
} | |
}; | |
pt lerp(const pt& a, const pt& b, double k) { | |
pt res; | |
res.x = a.x * (1.0-k) + k * b.x; | |
res.y = a.y * (1.0-k) + k * b.y; | |
return res; | |
} | |
#define SQ3 1.73205080757 // sqrt(3) | |
#define SQ3d2 0.86602540378 // sqrt(3)/2 | |
pt hex[6]; | |
int main() | |
{ | |
// options | |
double max_ring_radius = 1.0; | |
int n_rings = 6; | |
double tilt_angle = 0.0; | |
// ------------------------------------------------------------------------- | |
hex[0] = pt(1.0, 0.0); | |
hex[1] = pt(0.5, SQ3d2); | |
hex[2] = pt(-0.5, SQ3d2); | |
hex[3] = pt(-1.0, 0.0); | |
hex[4] = pt(-0.5, -SQ3d2); | |
hex[5] = pt(0.5, -SQ3d2); | |
// rotate | |
{ | |
double ca = cos(tilt_angle); | |
double sa = cos(tilt_angle); | |
for (int i=0; i<6; i++) { | |
double x = hex[i].x; | |
double y = hex[i].y; | |
hex[i].x = x*ca - y*sa; | |
hex[i].y = x*sa + y*ca; | |
} | |
} | |
// result | |
std::vector<pt> pts; | |
// magic | |
// (for each ring, for each edge, get edge's endpoints and gen samples on lerped points) | |
pts.push_back(pt(0.0,0.0)); | |
for (int i=1; i<=n_rings; i++) { | |
double ring_radius = max_ring_radius * (double)i/(double)n_rings; | |
for (int e=0; e<6; e++) { | |
pt a = hex[e]; | |
pt b = hex[(e+1)%6]; | |
for (int d=0; d<i; d++) { | |
pt p = lerp(a,b,(double)d/(double)i); | |
p.mul(ring_radius); | |
pts.push_back(p); | |
} | |
} | |
} | |
printf("%d\n" , pts.size()); | |
for (size_t i=0; i<pts.size(); i++) | |
printf("float2(%2.6f, %2.6f),\n", pts[i].x, pts[i].y); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment