Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Last active January 2, 2017 18:17
Show Gist options
  • Save KSXGitHub/ff3834bf84e2a9e6bf017d048ebf9480 to your computer and use it in GitHub Desktop.
Save KSXGitHub/ff3834bf84e2a9e6bf017d048ebf9480 to your computer and use it in GitHub Desktop.
Computer Graphics Revison
#! /usr/bin/env sh
echo "Input is '$1'"
echo "Creating '$1' from '$1.cpp'..."
g++-6 -std=c++14 -Wall -Wextra -o "$1" "$1.cpp" -lgraph && (
echo 'SUCCESS'
) || (
code=$?
echo 'FAILED' >&2
exit $code
)
#! /usr/bin/env sh
./build "$1" && (
echo "Executing '$1'..."
./"$1"
)
#include <iostream>
#include <cstdlib>
#include <graphics.h>
#include "include/range.cpp"
using namespace std;
typedef int Color;
struct Data {
int x, y, r;
};
int main ();
Data prompt ();
void graphmain (Data);
void begingraph ();
void endgraph ();
void circle (Data, Color);
void put8px (int, int, int, int, Color);
int main () {
graphmain(prompt());
return EXIT_SUCCESS;
}
Data prompt () {
Data data;
cout << "Enter center cordinate and radius: ";
cin >> data.x >> data.y >> data.r;
return data;
}
void graphmain (Data data) {
begingraph();
circle(data, YELLOW);
endgraph();
}
void begingraph () {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, NULL);
}
void endgraph () {
getch();
closegraph();
}
void circle (Data data, Color color) {
auto p = 1 - data.r;
auto x = 0;
auto y = data.r;
while (x < y) {
put8px(data.x, data.y, x, y, color);
if (p < 0) {
p += 2 * x + 3;
} else {
p += 2 * (x - y) + 5;
--y;
}
++x;
}
}
void put8px (int cx, int cy, int x, int y, Color color) {
putpixel(cx - x, cy - y, color);
putpixel(cy - y, cx - x, color);
putpixel(cx - x, cy + y, color);
putpixel(cy - y, cx + x, color);
putpixel(cx + x, cy - y, color);
putpixel(cy + y, cx - x, color);
putpixel(cx + x, cy + y, color);
putpixel(cy + y, cx + x, color);
}
def __circle (radius):
p = 1 - radius
x = 0
y = radius
while x < y:
yield (x, y)
if p < 0:
p += (x << 1) + 3
else:
p += ((x - y) << 1) + 5
y += 1
x += 1
def circle (cx, cy, radius):
iterable = ((dx, dy) for dx in (-cx, cx) for dy in (-cy, cy))
circleset = ((x, y, dx, dy) for x, y in __circle(radius) for dx, dy in iterable)
for x, y, dx, dy in circleset:
yield (x + dx, y + dy)
yield (x + dy, y + dx)
line circle
p0 2 * dy - dx 1 - R
p+ 2 * dy 2 * x + 3
p- 2 * (dy - dx) 2 * (x - y) + 5

Line

  • p0 = 2 * dy - dx
  • dp+ = 2 * dy
  • dp- = 2 * (dy - dx)

Circle

  • p0 = 5/4 - R
  • dp+ = 2 * x + 3
  • dp- = 2 * (x - y) + 5

Transformation

Translation

| 1 0 x |   | x |
| 0 1 y | * | y |
| 0 0 1 |   | 1 |

Scaling

| x 0 0 |   | x |
| 0 y 0 | * | y |
| 0 0 1 |   | 1 |

Rotation

|  c -s  0 |   | x |
|  s  c  0 | * | y |
|  0  0  1 |   | 1 |
c = cos α
s = sin α
#include <cstdlib>
#include <graphics.h>
int main ();
void graphmain ();
void initgraph ();
void circle ();
int main () {
graphmain();
return EXIT_SUCCESS;
}
void graphmain () {
initgraph();
circle();
getch();
closegraph();
}
void circle () {
auto x = getmaxx() >> 1;
auto y = getmaxy() >> 1;
auto r = (x < y ? x : y) >> 1;
circle(x, y, r);
}
void initgraph () {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, NULL);
}
#include <iostream>
#include <algorithm>
#include <graphics.h>
#include "include/range.cpp"
using namespace std;
typedef int Color;
typedef void (*PxFn) (int, int, Color);
const Color LINE_COLOR = BLUE;
int main ();
void prompt (int &, int &, int &, int &);
void graphmain (int, int, int, int);
void begingraph ();
void endgraph ();
void line (int, int, int, int, Color);
void _linexyp (int, int, int, int, Color, PxFn);
void _pixelpp (int, int, Color);
void _pixelnp (int, int, Color);
void _pixelpn (int, int, Color);
void _pixelnn (int, int, Color);
int main () {
int x1, y1, x2, y2;
prompt(x1, y1, x2, y2);
graphmain(x1, y1, x2, y2);
return 0;
}
void prompt (int & x1, int & y1, int & x2, int & y2) {
cout << "Enter begin point and end point coordinate: ";
cin >> x1 >> y1 >> x2 >> y2;
}
void graphmain (int x1, int y1, int x2, int y2) {
begingraph();
line(x1, y1, x2, y2, LINE_COLOR);
endgraph();
}
void begingraph () {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, NULL);
}
void endgraph () {
getch();
closegraph();
}
void line (int x1, int y1, int x2, int y2, Color color) {
if (x1 > x2) {
swap(x1, x2);
swap(y1, y2);
}
auto x = x1;
auto y = y1;
auto dx = x2 - x1;
auto dy = y2 - y1;
if (dx < dy) {
if (dx < 0) {
_linexyp(y, -x, dy, -dx, color, _pixelnn);
} else {
_linexyp(y, x, dy, dx, color, _pixelnp);
}
} else {
if (dy < 0) {
_linexyp(x, -y, dx, -dy, color, _pixelpn);
} else {
_linexyp(x, y, dx, dy, color, _pixelpp);
}
}
}
void _linexyp (int x, int y, int dx, int dy, Color color, PxFn px) {
auto p = 2 * dy - dx;
auto dpinc = 2 * dy;
auto dpdec = 2 * (dy - dx);
for (auto _x : Range(x, x + dx)) {
px(_x, y, color);
if (p < 0) {
p += dpinc;
} else {
p += dpdec;
++y;
}
}
}
void _pixelpp (int x, int y, Color color) {
putpixel(x, y, color);
}
void _pixelpn (int x, int y, Color color) {
putpixel(x, -y, color);
}
void _pixelnp (int x, int y, Color color) {
putpixel(y, x, color);
}
void _pixelnn (int x, int y, Color color) {
putpixel(y, -x, color);
}
def __line (x, y, dx, dy):
p = (dy << 1) - dx
dpinc = dy << 1
dpdec = (dy - dx) << 1
for x in range(x, x + dx):
yield (x, y)
if p < 0:
p += dpinc
else:
p += dpdec
y += 1
def line (x, y, dx, dy):
if dx < 0:
return ((-x, y) for x, y in line(-x, y, -dx, dy))
if dy < 0:
return ((x, -y) for x, y in line(x, -y, dx, -dy))
if dx < dy:
return ((y, x) for x, y in line(y, x, dy, dx))
def lineft (x1, y1, x2, y2):
return line(x1, y1, x2 - x1, y2 - y1)
#include <iostream>
#include "include/range.cpp"
using namespace std;
int main () {
for (auto x : Range(255)) {
cout << x << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <graphics.h>
int main ();
void graphmain ();
void initgraph ();
void circle ();
int main () {
using namespace std;
cout << "Hello, World!!\n";
cout << "Press ENTER to enter graphics mode... ";
cin.get();
graphmain();
return EXIT_SUCCESS;
}
void graphmain () {
initgraph();
circle();
getch();
closegraph();
}
void circle () {
auto x = getmaxx() >> 1;
auto y = getmaxy() >> 1;
auto r = (x < y ? x : y) >> 1;
circle(x, y, r);
}
void initgraph () {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment