File size: 7,431 Bytes
d0b8e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import gleam/list
import gleam/float
import gleam/int

pub type SketchStroke {
  SketchStroke(
    points: List(#(Float, Float)),
    color: String,
    thickness: Float,
    timestamp: String,
  )
}

pub type GhostWritingSession {
  GhostWritingSession(
    session_id: String,
    reference_image: String,
    user_strokes: List(SketchStroke),
    ghost_image_opacity: Float,
    current_page: Int,
    total_pages: Int,
  )
}

pub type SimilarityScore {
  SimilarityScore(
    overall_score: Float,
    structural_accuracy: Float,
    proportional_accuracy: Float,
    detail_completeness: Float,
    feedback: List(String),
  )
}

pub fn start_ghost_writing_session(
  image_url: String,
) -> GhostWritingSession {
  GhostWritingSession(
    session_id: "SESSION_2026",
    reference_image: image_url,
    user_strokes: [],
    ghost_image_opacity: 0.3,
    current_page: 1,
    total_pages: 1,
  )
}

pub fn create_stroke(
  points: List(#(Float, Float)),
  color: String,
  thickness: Float,
) -> SketchStroke {
  SketchStroke(
    points: points,
    color: color,
    thickness: thickness,
    timestamp: "2026-03-19T00:00:00Z",
  )
}

pub fn add_stroke_to_session(
  session: GhostWritingSession,
  stroke: SketchStroke,
) -> GhostWritingSession {
  GhostWritingSession(
    ..session,
    user_strokes: [stroke, ..session.user_strokes],
  )
}

pub fn adjust_ghost_opacity(
  session: GhostWritingSession,
  opacity: Float,
) -> GhostWritingSession {
  let clamped_opacity = case opacity {
    o if o <. 0.0 -> 0.0
    o if o >. 1.0 -> 1.0
    o -> o
  }
  GhostWritingSession(..session, ghost_image_opacity: clamped_opacity)
}

pub fn calculate_similarity(
  user_drawing: List(SketchStroke),
  _reference_image: String,
) -> SimilarityScore {
  let stroke_count = list.length(user_drawing)

  let structural_accuracy = case stroke_count {
    c if c > 15 -> 85.0
    c if c > 8 -> 65.0
    c if c > 3 -> 45.0
    _ -> 20.0
  }

  let proportional_accuracy = case stroke_count {
    c if c > 10 -> 80.0
    _ -> 50.0
  }

  let detail_completeness = case stroke_count {
    c if c > 15 -> 90.0
    c if c > 8 -> 70.0
    _ -> 40.0
  }

  let overall =
    {structural_accuracy +. proportional_accuracy +. detail_completeness} /. 3.0

  SimilarityScore(
    overall_score: overall,
    structural_accuracy: structural_accuracy,
    proportional_accuracy: proportional_accuracy,
    detail_completeness: detail_completeness,
    feedback: generate_drawing_feedback(overall, stroke_count),
  )
}

fn generate_drawing_feedback(score: Float, stroke_count: Int) -> List(String) {
  let base_feedback = case score {
    s if s >. 85.0 -> [
      "🏆 Exceptional job! Your drawing matches the reference perfectly!",
      "Your spatial understanding is excellent.",
    ]
    s if s >. 70.0 -> [
      "💪 Great work! Your sketch is very accurate.",
      "Minor details could be refined.",
    ]
    s if s >. 50.0 -> [
      "📈 Good effort! The main structure is recognizable.",
      "Try adding more detail.",
    ]
    _ -> [
      "Keep practicing! Reference the ghost image more carefully.",
      "Add more strokes to complete the drawing.",
    ]
  }

  case stroke_count {
    c if c < 3 ->
      list.append(base_feedback, ["Remember: Draw more strokes for better results!"])
    _ -> base_feedback
  }
}

pub fn generate_comparison_visualization(
  user_drawing: List(SketchStroke),
  _reference_image: String,
) -> String {
  let canvas = "<canvas id='comparison' width='800' height='600'></canvas>"
  let user_svg = render_strokes(user_drawing)
  "<div class='ghost-comparison'>" <> canvas <> user_svg <> "</div>"
}

fn render_strokes(strokes: List(SketchStroke)) -> String {
  "<svg class='user-drawing' width='800' height='600'>" <>
  list.fold(strokes, "", fn(acc, stroke) {
    acc <> render_stroke(stroke)
  }) <>
  "</svg>"
}

fn render_stroke(stroke: SketchStroke) -> String {
  case stroke.points {
    [] -> ""
    [first, ..rest] -> {
      let #(x, y) = first
      let path_start =
        "M " <> float.to_string(x) <> " " <> float.to_string(y)
      let path_rest =
        list.fold(rest, "", fn(acc, point) {
          let #(px, py) = point
          acc <> " L " <> float.to_string(px) <> " " <> float.to_string(py)
        })
      "<path d='" <>
      path_start <>
      path_rest <>
      "' stroke='" <>
      stroke.color <>
      "' stroke-width='" <>
      float.to_string(stroke.thickness) <>
      "' fill='none' stroke-linecap='round' />"
    }
  }
}

pub fn export_drawing_as_image(strokes: List(SketchStroke)) -> String {
  // Would generate PNG in production
  render_strokes(strokes)
}

pub fn compare_drawings(
  drawing1: List(SketchStroke),
  drawing2: List(SketchStroke),
) -> Float {
  // Simplified similarity metric
  let denominator =
    int.max(
      list.length(drawing1),
      list.length(drawing2),
    )

  case denominator == 0 {
    True -> 0.0
    False ->
      int.to_float(
        int.min(
          list.length(drawing1),
          list.length(drawing2),
        ),
      ) /. int.to_float(denominator) *. 100.0
  }
}

pub fn suggest_next_diagram(
  user_level: Float,
) -> String {
  case user_level {
    l if l >. 85.0 ->
      "Next: Advanced Anatomy (Heart Cross-Section)"
    l if l >. 70.0 ->
      "Next: Intermediate Diagram (Chemical Structure)"
    l if l >. 50.0 ->
      "Next: Basic Sketch (Simple Geometric Shape)"
    _ ->
      "Practice more with simpler diagrams first."
  }
}

pub fn track_drawing_history(session: GhostWritingSession) -> String {
  let stroke_count = list.length(session.user_strokes)
  "Drawing Progress: " <>
  int.to_string(stroke_count) <>
  " strokes | Page " <>
  int.to_string(session.current_page) <>
  "/" <>
  int.to_string(session.total_pages)
}

pub fn create_drawing_canvas_html() -> String {
  "
  <div class='ghost-writing-container'>
    <div class='ghost-layer'>
      <img id='ghostImg' src='' alt='Reference' class='ghost-image' />
      <canvas id='drawingCanvas' width='800' height='600' class='drawing-canvas'></canvas>
    </div>
    <div class='controls'>
      <label>Ghost Opacity: <input type='range' id='opacitySlider' min='0' max='100' value='30'></label>
      <button onclick='undoStroke()'>Undo</button>
      <button onclick='clearDrawing()'>Clear</button>
      <button onclick='submitDrawing()'>Submit</button>
    </div>
    <div id='feedback' class='feedback'></div>
  </div>"
}

pub fn analyze_drawing_curves(strokes: List(SketchStroke)) -> String {
  let smoothness =
    list.fold(strokes, 0.0, fn(acc, stroke) {
      acc +. calculate_curve_smoothness(stroke.points)
    }) /. int.to_float(list.length(strokes))

  "Curve Analysis: " <>
  float.to_string(smoothness) <>
  "% smoothness"
}

fn calculate_curve_smoothness(points: List(#(Float, Float))) -> Float {
  // Simplified - would use actual curvature analysis
  case list.length(points) {
    n if n > 10 -> 85.0
    n if n > 5 -> 70.0
    _ -> 50.0
  }
}

pub fn get_tracing_hints(
  _reference_image: String,
  current_progress: Float,
) -> List(String) {
  case current_progress {
    p if p <. 30.0 -> [
      "💡 Start by outlining the basic shape",
      "Focus on the main contours first",
    ]
    p if p <. 70.0 -> [
      "💡 Add the internal structures",
      "Pay attention to proportions",
    ]
    _ -> [
      "💡 Refine details and shading",
      "Your sketch looks great!",
    ]
  }
}