-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseScript.html
More file actions
156 lines (142 loc) · 4.22 KB
/
baseScript.html
File metadata and controls
156 lines (142 loc) · 4.22 KB
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
<!doctype html>
<html>
<head>
<title>Dynamic CindyJS Example</title>
<script src="https://cindyjs.org/dist/latest/Cindy.js"></script>
<style>
.content {
display: flex;
align-items: flex-start;
}
.input-section {
margin-left: 20px;
display: flex;
flex-direction: column;
}
#point-input {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="content">
<div id="cindy-container" style="width: 800px; height: 600px"></div>
<div class="input-section">
<!-- Textarea with initial points as editable example -->
<textarea id="point-input" rows="10" cols="33">
[
{"pos": [0, 0, 10]},
{"pos": [10, 0, 10]},
{"pos": [10, 10, 10]},
{"pos": [0, 10, 10]},
{"pos": [5, 5, 10]}
]
</textarea>
<button onclick="loadPoints()" id="load-points">Load Points</button>
<button onClick="addPoints()" id="add-points">Add Points</button>
</div>
</div>
<script>
let instance = null;
let initialPoints = [
{ pos: [0, 0, 10]},
{ pos: [10, 0, 10]},
{ pos: [10, 10, 10]},
{ pos: [0, 10, 10]},
{ pos: [5, 5, 10]},
];
function setupCindy(points) {
let geometry = convertPointsToGeometry(points);
if (instance) {
instance.shutdown(); // Shutdown the existing instance before reinitializing
}
instance = CindyJS({
canvasname: "cindy-container",
geometry: geometry,
});
}
function convertPointsToGeometry(pointsArray) {
let id = 0;
let cleanPointArray = [];
let points = [];
let joins = [];
let joinCounter = 1;
pointsArray.forEach((point) => {
if(!containsPointPosition(point, points)){
cleanPointArray.push(point);
points.push({
type: "Free",
name: id++,
pos: point.pos,
});
}
});
initialPoints = cleanPointArray;
radiusForCircle = getMaxDistance(points) + 2;
//drawcircle((0,0),1); //draws a circle with radius 1 and center (0,0)
for (let i = 0; i < points.length; i++) {
for (let j = i + 1; j < points.length; j++) {
let joinName = "j" + joinCounter++;
joins.push({
type: "Join",
name: joinName,
args: [points[i].name, points[j].name],
});
}
}
return points.concat(joins);
}
function getMaxDistance (points){
let totalDistance = 0;
points.forEach((point) =>{
let distance = 0;
for(let i = 0; i < 3; i++){
distance += Math.pow(point.pos[i],2);
}
distance = Math.sqrt(distance);
if(distance > totalDistance){
totalDistance = distance;
}
})
return totalDistance;
}
function containsPointPosition(point, points){
let isContaining = false;
points.some((p)=>{
if(p.pos[0]=== point.pos[0] && p.pos[1] === point.pos[1] && p.pos[2] === point.pos[2] ){
isContaining = true;
return true;
}
})
return isContaining;
}
function loadPoints() {
{
const inputValue =
document.getElementById("point-input").value;
try {
initialPoints = JSON.parse(inputValue);
setupCindy(initialPoints);
} catch (e) {
alert("Invalid JSON input");
}
}
}
function addPoints() {
const inputValue =
document.getElementById("point-input").value;
try {
const pointsData = JSON.parse(inputValue);
pointsData.forEach(point => {
initialPoints.push(point);
})
setupCindy(initialPoints);
} catch (e) {
alert("Invalid JSON input");
}
}
// Initialize with example points on page load
setupCindy(initialPoints);
</script>
</body>
</html>