Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spade shape #225

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data_morph/shapes/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ShapeFactory:
'rectangle': polygons.Rectangle,
'rings': circles.Rings,
'star': polygons.Star,
'spade': points.Spade,
}

AVAILABLE_SHAPES: list[str] = sorted(_SHAPE_MAPPING.keys())
Expand Down
63 changes: 63 additions & 0 deletions src/data_morph/shapes/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,66 @@ def distance(self, x: Number, y: Number) -> int:
Always returns 0 to allow for scattering of the points.
"""
return 0


class Spade(PointCollection):
"""
Class for the spade shape.

.. plot::
:scale: 75
:caption:
This shape is generated using the panda dataset.

from data_morph.data.loader import DataLoader
from data_morph.shapes.points import Spade

_ = Spade(DataLoader.load_dataset('panda')).plot()

Parameters
----------
dataset : Dataset
The starting dataset to morph into other shapes.
"""

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.data_bounds.x_bounds
y_bounds = dataset.data_bounds.y_bounds

# make heart points using Heart
heart_stack = Heart(dataset).points

# Graph heart curve
x_shift = sum(x_bounds) / 2
y_shift = sum(y_bounds) / 2

# vertical flip for heart
heart_y = -1.0 * (heart_stack[:, 1] - y_shift) + y_shift

# Here, I reference kevinr1299 codes
stefmolin marked this conversation as resolved.
Show resolved Hide resolved
# Graph line base
line_x = np.linspace(-6, 6, num=12)
line_y = np.repeat(-16, 12)

# Graph left wing
left_x = np.linspace(-6, 0, num=12)
left_y = 0.278 * np.power(left_x + 6.0, 2) - 16.0

# Graph right wing
right_x = np.linspace(0, 6, num=12)
right_y = 0.278 * np.power(right_x - 6.0, 2) - 16.0

# shift and scale the base and wing
x = np.concatenate((line_x, left_x, right_x), axis=0)
y = np.concatenate((line_y, left_y, right_y), axis=0)

# scale by the half the widest width of the spade
scale_factor = (x_bounds[1] - x_shift) / 16

x = x * scale_factor + x_shift
y = y * scale_factor + y_shift

x = np.concatenate((heart_stack[:, 0], x), axis=0)
y = np.concatenate((heart_y, y), axis=0)

super().__init__(*np.stack([x, y], axis=1))
Loading