Skip to content

Commit 174602d

Browse files
committed
updating logic algorithm and visual
1 parent 5aa656a commit 174602d

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

app/algorithm_dda.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import matplotlib.pyplot as plt
2-
import numpy as np
3-
from io import BytesIO
4-
import base64
1+
import plotly.graph_objects as go
2+
import plotly.io as pio
3+
import json
54

65
def dda_algorithm(x1, y1, x2, y2):
76
dx = x2 - x1
@@ -26,20 +25,13 @@ def generate_plot(x1, y1, x2, y2):
2625
points = dda_algorithm(x1, y1, x2, y2)
2726
x_values, y_values = zip(*points)
2827

29-
fig, ax = plt.subplots()
30-
ax.plot(x_values, y_values, marker='o', linestyle='-', color='b')
31-
ax.set_title(f'Line from ({x1}, {y1}) to ({x2}, {y2})')
32-
ax.set_xlabel('X')
33-
ax.set_ylabel('Y')
34-
ax.grid(True)
28+
fig = go.Figure()
29+
fig.add_trace(go.Scatter(x=x_values, y=y_values, mode='lines+markers', name='Line'))
30+
fig.update_layout(title=f'Line from ({x1}, {y1}) to ({x2}, {y2})',
31+
xaxis_title='X',
32+
yaxis_title='Y',
33+
template='plotly_dark')
3534

36-
# Save plot to a BytesIO object
37-
buffer = BytesIO()
38-
plt.savefig(buffer, format='png')
39-
plt.close(fig)
40-
buffer.seek(0)
35+
plot_json = pio.to_json(fig)
4136

42-
# Encode image to base64
43-
img_str = base64.b64encode(buffer.read()).decode('utf-8')
44-
45-
return img_str
37+
return plot_json

app/templates/form.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<!-- linevisualizer/templates/linevisualizer/form.html -->
1+
22
<!DOCTYPE html>
33
<html>
44
<head>
55
<title>Draw Line</title>
6+
7+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
68
</head>
79
<body>
810
<h1>Draw Line Using DDA Algorithm</h1>
@@ -18,9 +20,13 @@ <h1>Draw Line Using DDA Algorithm</h1>
1820
<input type="number" id="y2" name="y2" required>
1921
<button type="submit">Draw Line</button>
2022
</form>
21-
{% if img_str %}
22-
<h2>Line Plot</h2>
23-
<img src="data:image/png;base64,{{ img_str }}" alt="Line Plot">
23+
{% if plot_json %}
24+
<h2>Interactive Line Plot</h2>
25+
<div id="plot"></div>
26+
<script type="text/javascript">
27+
var plotJson = {{ plot_json|safe }};
28+
Plotly.newPlot('plot', plotJson.data, plotJson.layout);
29+
</script>
2430
{% endif %}
2531
</body>
2632
</html>

app/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from .algorithm_dda import generate_plot
44

55
def draw_line(request):
6-
img_str = None
6+
plot_json = None
77
if request.method == 'POST':
88
x1 = int(request.POST.get('x1'))
99
y1 = int(request.POST.get('y1'))
1010
x2 = int(request.POST.get('x2'))
1111
y2 = int(request.POST.get('y2'))
1212

13-
img_str = generate_plot(x1, y1, x2, y2)
13+
plot_json = generate_plot(x1, y1, x2, y2)
1414

15-
return render(request, 'form.html', {'img_str': img_str})
15+
return render(request, 'form.html', {'plot_json': plot_json})

0 commit comments

Comments
 (0)