-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 28ddde8
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ridger |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[project] | ||
name = "ridger" | ||
version = "0.1.0" | ||
description = "Create ridge images" | ||
authors = [ | ||
{name = "Jacopo Farina",email = "[email protected]"}, | ||
] | ||
dependencies = [ | ||
"Pillow>=10.1.0", | ||
] | ||
requires-python = ">=3.12" | ||
readme = "README.md" | ||
license = {text = "MIT"} | ||
|
||
[build-system] | ||
requires = ["setuptools>=61", "wheel"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[tool.pdm.dev-dependencies] | ||
dev = [ | ||
"ruff>=0.1.6", | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Callable | ||
|
||
from PIL import Image, ImageDraw | ||
|
||
IMSIZE = (1000, 1000) | ||
BGCOLOR = (0, 0, 0, 255) | ||
# distance between line in data space | ||
LINE_DISTANCE = 11 | ||
# distance between data points on the same line in data space | ||
DATA_POINT_DISTANCE = 5 | ||
|
||
|
||
def generate_ridge_image( | ||
height_map: Callable[[int, int], int], | ||
line_color: Callable[[int, int, int, int], tuple[int, int, int, int]], | ||
imsize=IMSIZE, | ||
bgcolor=BGCOLOR, | ||
data_to_screen=lambda x, y: (x, y), | ||
): | ||
im = Image.new("RGBA", imsize, bgcolor) | ||
|
||
draw = ImageDraw.Draw(im) | ||
for line_depth in range(LINE_DISTANCE, imsize[1], LINE_DISTANCE): | ||
previous_point = None, None | ||
for x in range(0, imsize[0], DATA_POINT_DISTANCE): | ||
height = height_map(x, line_depth) | ||
# first data point is skipped | ||
if previous_point[0] is not None: | ||
# draw the trapezoid that occludes the previous lines | ||
draw.polygon( | ||
[ | ||
# previous point, base | ||
data_to_screen(x - DATA_POINT_DISTANCE, line_depth), | ||
# previous point, with elevation | ||
data_to_screen(*previous_point), | ||
# current point, with elevation | ||
data_to_screen(x, line_depth - height), | ||
# current point, base | ||
data_to_screen(x, line_depth), | ||
], | ||
fill=bgcolor, | ||
) | ||
# draw the line over the trapezoid | ||
draw.line( | ||
( | ||
data_to_screen(*previous_point), | ||
data_to_screen(x, line_depth - height), | ||
), | ||
fill=line_color(*previous_point, x, line_depth - height), | ||
width=2, | ||
) | ||
previous_point = x, line_depth - height | ||
return im |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from math import sqrt | ||
from ridger import generate_ridge_image, IMSIZE | ||
|
||
|
||
def height_map(x, y): | ||
dist = sqrt((x - 500) ** 2 + (y - 500) ** 2) | ||
# donut hole | ||
if dist < 100: | ||
return 0 | ||
# outside the donut | ||
if dist > 300: | ||
return 0 | ||
return 50 | ||
|
||
|
||
def line_color(x1, y1, x2, y2): | ||
if y1 == y2: | ||
return (255, 255, 255, 255) | ||
elif y1 < y2: | ||
return (255, 0, 0, 255) | ||
else: | ||
return (0, 255, 0, 255) | ||
|
||
|
||
def data_to_screen(x, y) -> tuple[int, int]: | ||
# how long is the far away side? | ||
horizon_min_side = 600 | ||
horizon_max_side = 1000 | ||
# how long is here? | ||
this_horizon = ( | ||
horizon_min_side + (horizon_max_side - horizon_min_side) * y / IMSIZE[1] | ||
) | ||
# scale x to the current hporizon keeping the center | ||
x = IMSIZE[0] / 2 + (x - IMSIZE[0] / 2) * this_horizon / horizon_max_side | ||
return x, y | ||
|
||
|
||
generate_ridge_image( | ||
height_map, line_color, bgcolor=(128, 128, 128, 255), data_to_screen=data_to_screen | ||
).show() |