Code Sketch
yoi
Category: Programming
// =======================================================
// DELAUNAY TRIANGULATION STUDIO
// ADVANCED EDITION
// BY YADNESH
// =======================================================
import scala.collection.mutable.ArrayBuffer
cleari()
disablePanAndZoom()
// ---------------- BACKGROUND ----------------
setBackground(cm.darkSlateGray)
val cb = canvasBounds
val toolbarHeight = 88.0
val footerHeight = 28.0
val workBottom = cb.y + footerHeight
val workTop = cb.y + cb.height - toolbarHeight
val workHeight = workTop - workBottom
// ---------------- SETTINGS ----------------
val maxPoints = 250
var showPoints = true
var showGrid = true
var triangleCount = 0
val palette = Array(
cm.cyan,
cm.magenta,
cm.orange,
cm.green,
cm.blue,
cm.yellow
)
// ---------------- POINT STORAGE ----------------
val points = ArrayBuffer[Point]()
def resetPoints() {
points.clear()
points.append(
Point(cb.x + cb.width * 0.28,
workBottom + workHeight * 0.30)
)
points.append(
Point(cb.x + cb.width * 0.72,
workBottom + workHeight * 0.28)
)
points.append(
Point(cb.x + cb.width * 0.70,
workBottom + workHeight * 0.73)
)
points.append(
Point(cb.x + cb.width * 0.30,
workBottom + workHeight * 0.69)
)
}
resetPoints()
// ---------------- POINT SYSTEM ----------------
def addUniquePoint(x: Double, y: Double) {
val tooClose = points.exists { p =>
val dx = p.x - x
val dy = p.y - y
dx * dx + dy * dy < 100.0
}
if (!tooClose && points.size < maxPoints) {
points.append(Point(x, y))
}
}
// ---------------- RANDOM GENERATOR ----------------
def addRandomPoints(n: Int) {
var i = 0
val usableW = math.max(1, (cb.width - 50).toInt)
val usableH = math.max(1,
(workHeight - 40).toInt
)
while (i < n && points.size < maxPoints) {
val rx = cb.x + 25 + random(usableW)
val ry = workBottom + 20 + random(usableH)
addUniquePoint(rx, ry)
i += 1
}
}
// ---------------- GRID SYSTEM ----------------
def drawGrid() {
if (showGrid) {
var gx = cb.x
while (gx <= cb.x + cb.width) {
val gridLine = Picture {
setPenColor(cm.darkGray)
setPenThickness(0.6)
setPosition(gx, workBottom)
lineTo(gx, workTop)
}
draw(gridLine)
gx += 40.0
}
var gy = workBottom
while (gy <= workTop) {
val gridLine = Picture {
setPenColor(cm.darkGray)
setPenThickness(0.6)
setPosition(cb.x, gy)
lineTo(cb.x + cb.width, gy)
}
draw(gridLine)
gy += 40.0
}
}
}
// ---------------- BUTTON SETTINGS ----------------
val buttonWidth = 72.0
val buttonHeight = 25.0
val buttonGap = 10.0
val totalButtonWidth =
buttonWidth * 6 + buttonGap * 5
val buttonStart =
cb.x + (cb.width - totalButtonWidth) / 2 +
buttonWidth / 2
val buttonY = cb.y + cb.height - 58.0
val randomX = buttonStart
val undoX = buttonStart +
buttonWidth + buttonGap
val clearX = buttonStart +
2 * (buttonWidth + buttonGap)
val dotsX = buttonStart +
3 * (buttonWidth + buttonGap)
val gridX = buttonStart +
4 * (buttonWidth + buttonGap)
val resetX = buttonStart +
5 * (buttonWidth + buttonGap)
// ---------------- BUTTON DRAWING ----------------
def drawButton(
cx: Double,
cy: Double,
label: String,
color: Color
) {
val button =
fillColor(color) *
penColor(cm.white) ->
Picture.rectangle(buttonWidth, buttonHeight)
button.setPosition(cx, cy)
draw(button)
val text =
penColor(cm.white) ->
Picture.text(label, 12)
text.setPosition(cx, cy - 4)
draw(text)
}
// ---------------- BUTTON CLICK CHECK ----------------
def buttonHit(
x: Double,
y: Double,
cx: Double
): Boolean = {
math.abs(x - cx) <= buttonWidth / 2 &&
math.abs(y - buttonY) <= buttonHeight / 2
}
// ---------------- TOOLBAR ----------------
def drawToolbar() {
val topPanel =
fillColor(cm.black) *
penColor(cm.cyan) ->
Picture.rectangle(cb.width, toolbarHeight)
topPanel.setPosition(
cb.x + cb.width / 2,
cb.y + cb.height - toolbarHeight / 2
)
draw(topPanel)
val title =
penColor(cm.cyan) ->
Picture.text(
"DELAUNAY TRIANGULATION STUDIO",
20
)
title.setPosition(
cb.x + cb.width / 2,
cb.y + cb.height - 19
)
draw(title)
drawButton(
randomX,
buttonY,
"RANDOM",
cm.blue
)
drawButton(
undoX,
buttonY,
"UNDO",
cm.orange
)
drawButton(
clearX,
buttonY,
"CLEAR",
cm.red
)
drawButton(
dotsX,
buttonY,
"DOTS",
if (showPoints) cm.green else cm.gray
)
drawButton(
gridX,
buttonY,
"GRID",
if (showGrid) cm.green else cm.gray
)
drawButton(
resetX,
buttonY,
"RESET",
cm.magenta
)
}
// ---------------- POINT DRAWING ----------------
def drawPoints() {
if (showPoints) {
points.foreach { p =>
// Outer glowing point
val outer =
fillColor(cm.cyan) *
penColor(cm.white) ->
Picture.circle(6)
outer.setPosition(p.x, p.y)
draw(outer)
// Inner point
val inner =
fillColor(cm.white) *
penColor(cm.white) ->
Picture.circle(2.2)
inner.setPosition(p.x, p.y)
draw(inner)
}
}
}
// ---------------- TRIANGULATION ENGINE ----------------
def drawTriangles() {
triangleCount = 0
if (points.size > 3) {
val trs = triangulate(points)
triangleCount = trs.size
var index = 0
trs.foreach { t =>
val triangleColor =
palette(index % palette.length)
val gradient =
cm.linearGradient(
t.a.x,
t.a.y,
triangleColor,
t.b.x,
t.b.y,
cm.darkSlateGray
)
val triangle = Picture {
setFillColor(gradient)
setPenColor(cm.cyan)
setPenThickness(1.2)
setPosition(t.a.x, t.a.y)
lineTo(t.b.x, t.b.y)
lineTo(t.c.x, t.c.y)
// Close triangle for filling
lineTo(t.a.x, t.a.y)
}
draw(triangle)
index += 1
}
}
}
// ---------------- FOOTER / STATISTICS ----------------
def drawStatistics() {
val footer =
fillColor(cm.black) *
penColor(cm.cyan) ->
Picture.rectangle(cb.width, footerHeight)
footer.setPosition(
cb.x + cb.width / 2,
cb.y + footerHeight / 2
)
draw(footer)
val status =
penColor(cm.white) ->
Picture.text(
"POINTS: " + points.size +
" | TRIANGLES: " + triangleCount +
" | GRID: " + (if (showGrid) "ON" else "OFF") +
" | DOTS: " + (if (showPoints) "ON" else "OFF"),
12
)
status.setPosition(
cb.x + cb.width / 2,
cb.y + 9
)
draw(status)
}
// ---------------- MAIN RENDER ENGINE ----------------
def drawScene() {
erasePictures()
// Background grid
drawGrid()
// Delaunay triangles
drawTriangles()
// Glowing points
drawPoints()
// UI
drawToolbar()
// Live statistics
drawStatistics()
}
// ---------------- START ----------------
drawScene()
// ---------------- MOUSE CONTROLS ----------------
onMouseClick { (x, y) =>
if (y >= workTop) {
// RANDOM BUTTON
if (buttonHit(x, y, randomX)) {
addRandomPoints(20)
}
// UNDO BUTTON
else if (buttonHit(x, y, undoX)) {
if (points.size > 0) {
points.remove(points.size - 1)
}
}
// CLEAR BUTTON
else if (buttonHit(x, y, clearX)) {
points.clear()
}
// DOTS BUTTON
else if (buttonHit(x, y, dotsX)) {
showPoints = !showPoints
}
// GRID BUTTON
else if (buttonHit(x, y, gridX)) {
showGrid = !showGrid
}
// RESET BUTTON
else if (buttonHit(x, y, resetX)) {
resetPoints()
}
}
// ADD POINT TO CANVAS
else if (y > workBottom && y < workTop) {
addUniquePoint(x, y)
}
// REFRESH EVERYTHING
drawScene()
}
// =======================================================
// END OF PROJECT
// =======================================================