about summary refs log tree commit diff stats
path: root/src/algorithms/line-drawing/DDA.svelte
blob: 379e965f787a07c2ac416f5384e9fb715d709ac8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<script>
  import { onMount } from "svelte";
  import Plotly from "plotly.js-basic-dist-min";

  let x0 = 0, y0 = 0, x1 = 4, y1 = 4;
  let dx, dy;

  let steps;
  let x_inc, y_inc;

  let x_axis = [], y_axis = [];
  let x_axis_floored = [], y_axis_floored = [];

  function dda() {
    dx = Math.abs(x1 - x0);
    dy = Math.abs(y1 - y0);

    steps = Math.max(...[dx, dy].map(x => Math.abs(x)));

    x_inc = dx / steps;
    y_inc = dy / steps;

    // Build the x axis.
    x_axis = [];
    let X = x0;
    for (let idx = 0; idx < steps; idx++) {
      X += x_inc;
      x_axis.push(X.toPrecision(3));
    }
    x_axis_floored = x_axis.map(function(x) {return Math.round(x)});

    // Build the y axis.
    y_axis = [];
    let Y = y0;
    for (let idx = 0; idx < steps; idx++) {
      Y += y_inc;
      y_axis.push(Y.toPrecision(3));
    }
    y_axis_floored = y_axis.map(function(y) {return Math.round(y)});

    // Plot the chart.
    Plotly.newPlot('algoChart', [{
      x: x_axis_floored,
      y: y_axis_floored,
      type: 'lines',
      name: 'DDA'
    }]);
  }

  onMount(() => {
    dda();
  });
</script>

<h2>Digital differential analyzer</h2>
<form class="points">
  <label for="x0">x0: </label>
  <input type="number" id="x0" name="x0" on:change={dda} bind:value={x0}>&nbsp;
  <label for="y0">y0: </label>
  <input type="number" id="y0" name="y0" on:change={dda} bind:value={y0}>
  <br>
  <label for="x1">x1: </label>
  <input type="number" id="x1" name="x1" on:change={dda} bind:value={x1}>&nbsp;
  <label for="y1">y1: </label>
  <input type="number" id="y1" name="y1" on:change={dda} bind:value={y1}>
  <br>
  <button type="button" on:click={dda}>Solve</button>
</form>

<hr>
<h3>Solution</h3>

<label for="dx">dx = <code>|x1 - x0|</code>: </label>
<input type="text" id="dx" name="dx" value={dx} disabled><br>
<label for="dy">dy = <code>|y1 - y0|</code>: </label>
<input type="text" id="dy" name="dy" value={dy} disabled>
<br>

<label for="steps">steps: </label>
<input type="text" id="steps" name="steps" value={steps} disabled>
<br>

<label for="x_inc">x Increment = <code>dx / steps</code>: </label>
<input type="text" id="x_inc" name="x_inc" value={x_inc} disabled><br>
<label for="y_inc">y Increment = <code>dy / steps</code>: </label>
<input type="text" id="y_inc" name="y_inc" value={y_inc} disabled>

<details>
  <summary>
    Table
  </summary>
  <table>
    <thead>
      <tr>
        <th>Iteration</th>
        <th>x</th>
        <th>y</th>
        <th>x (plot)</th>
        <th>y (plot)</th>
      </tr>
    </thead>

    <tbody>
      {#each x_axis as _, i}
        <tr>
          <td>{i + 1}</td>
          <td>{x_axis[i]}</td>
          <td>{y_axis[i]}</td>
          <td>{x_axis_floored[i]}</td>
          <td>{y_axis_floored[i]}</td>
        </tr>
      {/each}
    </tbody>
  </table>
</details>

<h3>Graph</h3>
<div id="algoChart"></div>

<style>
  hr { color: var(--bg-hl-alt-intense); }
  button {
    margin: .8em;
    padding: 0.4em 1.2em;
    width: 100%;
  }
  details {
    border: 1px solid var(--bg-alt);
    border-radius: 4px;
    padding: 0.8em;
    margin: 1em;
  }
</style>