Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file modified lawn_simulation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 76 additions & 53 deletions lawnsim/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lawn_area import LawnRectangle
from location import Location
from spigot import Spigot
from sprinkler import Sprinkler
from sprinkler import Sprinkler, OscillatingSprinkler


lavender_rectangle = LawnRectangle(
Expand Down Expand Up @@ -39,55 +39,42 @@
)
hydrangea_sprinkler = Sprinkler(
location=Location(x=29.0, y=8.0),
spray_arc=190.0,
spray_arc=200.0,
spray_direction=180,
spray_radius=8.5,
spray_radius=8.0,
water_flow_rate=40,
)

tulip_sprinkler = Sprinkler(
location=Location(x=15.0, y=15.0),
spray_arc=190.0,
location=Location(x=12.0, y=15.0),
spray_arc=190,
spray_direction=270,
spray_radius=10.0,
spray_radius=12.5,
water_flow_rate=40,
)
walkway_spigot.connect(sink=hydrangea_hose)
hydrangea_hose.connect(sink=hydrangea_sprinkler)
hydrangea_sprinkler.connect(sink=tulip_hose)
tulip_hose.connect(sink=tulip_sprinkler)


little_lavender_hose = Hose(
length=10.0,
)
corner_sprinkler = Sprinkler(
location=Location(x=0.0, y=0.0),
spray_arc=90.0,
spray_direction=45,
spray_radius=14.5,
)
lavender_hose = Hose(length=25.0)
tulip_sprinkler.connect(sink=lavender_hose)

little_lavender_hose = Hose(length=10.0)
side_spigot.connect(sink=little_lavender_hose)
little_lavender_hose.connect(sink=corner_sprinkler)
lavender_hose = Hose(
length=25.0,
)
lavender_sprinkler = Sprinkler(
location=Location(x=0.0, y=15.0),
spray_arc=90,
spray_direction=315,
spray_radius=14.0,
oscillating_sprinkler = OscillatingSprinkler(
location=Location(x=8.0, y=10.0),
orientation=0.0,
width=15.0,
length=8.0,
water_flow_rate=80.0,
)
corner_sprinkler.connect(sink=lavender_hose)
lavender_hose.connect(sink=lavender_sprinkler)

little_lavender_hose.connect(sink=oscillating_sprinkler)

spigots = [walkway_spigot, side_spigot]
hoses = [hydrangea_hose, tulip_hose, little_lavender_hose, lavender_hose]
sprinklers = [
hydrangea_sprinkler,
tulip_sprinkler,
corner_sprinkler,
lavender_sprinkler,
]
hoses = [hydrangea_hose, tulip_hose, little_lavender_hose]
sprinklers = [hydrangea_sprinkler, tulip_sprinkler, oscillating_sprinkler]

lawn = Lawn(
rectangles=rectangles,
Expand Down Expand Up @@ -129,25 +116,61 @@ def plot(self):

# Plot sprinklers and their spray arcs
for sprinkler in self.lawn.sprinklers:
ax.plot(sprinkler.location.x, sprinkler.location.y, "ro")
total_area = (
np.pi * sprinkler.spray_radius**2 * (sprinkler.spray_arc / 360.0)
)
water_per_area = sprinkler.water_flow_rate / total_area
alpha_value = water_per_area # Adjust this calculation based on your needs

sprinkler_patch = patches.Wedge(
(sprinkler.location.x, sprinkler.location.y),
sprinkler.spray_radius,
sprinkler.spray_direction - sprinkler.spray_arc / 2,
# theta1 (start angle)
sprinkler.spray_direction + sprinkler.spray_arc / 2,
# theta2 (end angle)
fill=True,
color="blue",
alpha=alpha_value,
)
ax.add_patch(sprinkler_patch)
if isinstance(sprinkler, OscillatingSprinkler):
# For oscillating sprinklers, plot a rectangle
t = (
Affine2D().rotate_deg_around(
sprinkler.location.x,
sprinkler.location.y,
sprinkler.orientation,
)
+ ax.transData
)
rectangle = patches.Rectangle(
(
sprinkler.location.x - sprinkler.width / 2,
sprinkler.location.y - sprinkler.length / 2,
),
sprinkler.width,
sprinkler.length,
fill=True,
color="blue",
alpha=0.3,
transform=t,
)
ax.add_patch(rectangle)

# Add a small rectangle to represent the sprinkler
sprinkler_marker = patches.Rectangle(
(sprinkler.location.x - 0.25, sprinkler.location.y - 1.25),
0.5,
2.5,
fill=True,
color="red",
)
ax.add_patch(sprinkler_marker)
else:
ax.plot(sprinkler.location.x, sprinkler.location.y, "ro")
total_area = (
np.pi * sprinkler.spray_radius**2 * (sprinkler.spray_arc / 360.0)
)
water_per_area = sprinkler.water_flow_rate / total_area
alpha_value = (
water_per_area # Adjust this calculation based on your needs
)

sprinkler_patch = patches.Wedge(
(sprinkler.location.x, sprinkler.location.y),
sprinkler.spray_radius,
sprinkler.spray_direction - sprinkler.spray_arc / 2,
# theta1 (start angle)
sprinkler.spray_direction + sprinkler.spray_arc / 2,
# theta2 (end angle)
fill=True,
color="blue",
alpha=alpha_value,
)
ax.add_patch(sprinkler_patch)

# Set the aspect of the plot to match the real-world aspect
ax.set_aspect("equal")
Expand Down
10 changes: 10 additions & 0 deletions lawnsim/sprinkler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ class Sprinkler(
sink: WaterSink = None
source: WaterSource = None
water_flow_rate: float = 40.0


@dataclass
class OscillatingSprinkler(WaterSink):
location: Location
orientation: float
width: float
length: float
source: WaterSource = None
water_flow_rate: float = 40.0