-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithmpatterngeneration.html
146 lines (120 loc) · 4.71 KB
/
Algorithmpatterngeneration.html
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
<!DOCTYPE html>
<html>
<head>
<title>Pattern Generator</title>
<style>
#generatorCanvas {
border: 1px solid black;
opacity: 0;
transition: opacity 1s ease-in-out;
}
</style>
</head>
<body>
<h1>Pattern Generator</h1>
<div id="outputBox"></div>
<canvas id="generatorCanvas" width="400" height="400"></canvas>
<canvas id="finalCanvas" width="400" height="400"></canvas>
<script>
var gridSize = 20;
var cellSize = 20;
var mazeGrid = [];
var patternCounter = 0;
var currentCell = { x: 0, y: 0 };
var generatedPatterns = [];
var controllerCell = { x: 0, y: 0 };
var colliders = [];
window.addEventListener("load", function() {
setInterval(generateAndSort, 1000); // Generate and sort patterns every second
window.addEventListener("keydown", moveControllerCell); // Add keyboard controls
});
function generateAndSort() {
var canvas = document.getElementById("generatorCanvas");
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
// Generate pattern
for (var x = 0; x < canvas.width; x += cellSize) {
for (var y = 0; y < canvas.height; y += cellSize) {
var centerX = x + cellSize / 2;
var centerY = y + cellSize / 2;
// Calculate unique values for each cell based on randomized mathematical equations
var value1 = Math.random();
var value2 = Math.random();
var value3 = Math.random();
var value = value1 + value2 + value3;
// Map the value to a hexadecimal color
var colorValue = Math.floor((value + 1) * 128);
var color = "#" + colorValue.toString(16).padStart(2, "0").repeat(3);
context.fillStyle = color;
context.fillRect(x, y, cellSize, cellSize);
}
}
// Generate maze
mazeGrid = createMazeGrid();
drawMaze(context);
// Save pattern to array
generatedPatterns.push(canvas.toDataURL());
// Sort patterns
generatedPatterns.sort();
// Display patterns on final canvas
var finalCanvas = document.getElementById("finalCanvas");
var finalContext = finalCanvas.getContext("2d");
finalContext.clearRect(0, 0, finalCanvas.width, finalCanvas.height);
generatedPatterns.forEach(function(pattern, index) {
var image = new Image();
image.src = pattern;
image.onload = function() {
finalContext.drawImage(image, index * cellSize, 0, cellSize, cellSize);
};
});
patternCounter++;
// Output pattern data
var outputBox = document.getElementById("outputBox");
outputBox.textContent = "Generated " + patternCounter + " patterns";
}
// Function to create the maze grid
function createMazeGrid() {
var grid = [];
for (var i = 0; i < gridSize; i++) {
var row = [];
for (var j = 0; j < gridSize; j++) {
row.push({ x: j, y: i, visited: false });
}
grid.push(row);
}
// Maze generation logic goesHere is the continuation of the script:
// here
return grid;
}
// Function to draw the maze on the canvas
function drawMaze(context) {
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
var cell = mazeGrid[i][j];
if (cell.visited) {
context.fillStyle = "rgba(255, 0, 0, 0.5)"; // Highlight current cell in red with 50% opacity
context.fillRect(cell.x * cellSize, cell.y * cellSize, cellSize, cellSize);
}
}
}
}
// Function to move the controller cell
function moveControllerCell(event) {
switch (event.key) {
case "w":
controllerCell.y--;
break;
case "a":
controllerCell.x--;
break;
case "s":
controllerCell.y++;
break;
case "d":
controllerCell.x++;
break;
}
}
</script>
</body>
</html>