Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active December 26, 2015 18:09
Show Gist options
  • Save Heimdell/7192587 to your computer and use it in GitHub Desktop.
Save Heimdell/7192587 to your computer and use it in GitHub Desktop.
source image
============
.........33.........
........3223........
.......321123.......
......321..123......
.....321....123.....
....321......123....
...3217......8123...
..321..7....8..123..
.321....7..8....123.
321......78......123
645......87......546
.645....8..7....546.
..645..8....7..546..
...6458......7546...
....645......546....
.....645....546.....
......645..546......
.......645546.......
........6446........
.........66.........
rows
====
7......546???????????
77......546??????????
77.....546.??????????
77.....54..??????????
77.....46..??????????
77....54...??????????
77....56....?????????
777...44....?????????
777...56.....????????
777..756......???????
77777744.......??????
777..756......???????
777...56.....????????
777...44....?????????
77....56....?????????
77....54...??????????
77.....46..??????????
77.....54..??????????
77.....546.??????????
77......546??????????
78......546??????????
78......546??????????
78.....546.??????????
78.....54..??????????
78.....46..??????????
78....54...??????????
78....56....?????????
788...44....?????????
788...56.....????????
788..856......???????
78888844.......??????
788..856.......??????
788...56......???????
788...44.....????????
78....56.....????????
78....54....?????????
78.....46...?????????
78.....54...?????????
78.....546..?????????
78......5466?????????
77......1233?????????
77......1233?????????
77.....123..?????????
77.....12...?????????
77.....23...?????????
77....12....?????????
77....13.....????????
777...22.....????????
777...13......???????
777..713.......??????
77777722........?????
777..713.......??????
777...13......???????
777...22.....????????
77....13.....????????
77....12....?????????
77.....23...?????????
77.....12...?????????
77.....123..?????????
77......1233?????????
78......1233?????????
78......1233?????????
78.....123..?????????
78.....12...?????????
78.....23...?????????
78....12....?????????
78....13.....????????
788...22.....????????
788...13......???????
788..813.......??????
78888822.......??????
788..813......???????
788...13.....????????
788...22....?????????
78....13....?????????
78....12...??????????
78.....23..??????????
78.....12..??????????
78.....123.??????????
78......123??????????
image after transforming into rows and back
===========================================
.........33.........
........3223........
.......321123.......
......321..123......
.....321....123.....
....321......123....
...3217......8123...
..321..7....8..123..
.321....7..8....123.
321......78......123
645......87......546
.645....8..7....546.
..645..8....7..546..
...6458......7546...
....645......546....
.....645....546.....
......645..546......
.......645546.......
........6446........
.........66.........
#include <math.h>
#include <memory.h>
#include <iostream>
#include <vector>
using namespace std;
template <int WIDTH, int HEIGHT, class Cell = char, Cell empty = '?'>
struct Image
{
Cell raw[WIDTH][HEIGHT];
Image() {
memset(raw, '.', WIDTH * HEIGHT);
}
template <int w1, int h1>
void blit(char * img) {
for (int i = 0; i < w1; i++)
for (int j = 0; j < h1; j++)
raw [i][j] = img[j * w1 + i];
}
Cell getRadial(float angle, float shift) {
int x = WIDTH / 2, y = HEIGHT / 2;
x += sin(angle) * shift;
y += cos(angle) * shift;
if (x < 0 || x >= WIDTH
|| y < 0 || y >= HEIGHT)
{
return empty;
}
return raw[x][y];
}
void putRadial(Cell cell, float angle, float shift) {
int x = WIDTH / 2, y = HEIGHT / 2;
x += sin(angle) * shift;
y += cos(angle) * shift;
if (x < 0 || x >= WIDTH
|| y < 0 || y >= HEIGHT)
{
return;
}
raw[x][y] = cell;
}
void dump(string header) {
cout << header << endl;
for (int i = 0; i < header.size(); i++)
cout << "=";
cout << endl;
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
cout << raw[i][j];
}
cout << "\n";
}
cout << endl;
}
string row(float angle, int rad, float step = 1) {
string accum;
for (int i = 0; i <= rad; i++) {
accum += getRadial(angle, i * step);
}
return accum;
}
};
int main(int argc, char const *argv[])
{
Image<20, 20> image;
Image<20, 20> image2;
image.blit<20, 20>(
".........36........."
"........3246........"
".......321546......."
"......321..546......"
".....321....546....."
"....321......546...."
"...3217......8546..."
"..321..7....8..546.."
".321....7..8....546."
"321......78......546"
"321......87......546"
".321....8..7....546."
"..321..8....7..546.."
"...3218......7546..."
"....321......546...."
".....321....546....."
"......321..546......"
".......321546......."
"........3246........"
".........36........."
);
image.dump("source image");
cout << "rows\n====" << endl;
vector<string> rows;
for (float angle = 0; angle < M_PI * 2; angle += M_PI / 40) {
rows.push_back(image.row(angle, 15));
cout << image.row(angle, 20) << "\n";
}
cout << endl;
int i = 0;
for (float angle = 0; angle < M_PI * 2; angle += M_PI / 40, i++) {
for (int j = 0; j < 15; j++) {
image2.putRadial(rows[i][j], angle, j);
}
}
image2.dump("image after transforming into rows and back");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment