出現報錯:Uncaught TypeError: can't access property "false", colors[shape.type] is undefined generateTiling
查找原因並修復。
Re: 一個新的遊戲創意
gemini 3.1 pro
代码: 全选
### **錯誤原因分析**
這個錯誤是因為**物件屬性名稱不一致**所造成的。
我們來追蹤一下你的程式碼:
1. 當你建立**第一個(根)圖形**時,你賦予它的屬性名稱是 `type`:
```javascript
shapes.push({ type: 'A', mirrored: false, vertices: rootVertices });
```
2. 但是,當程式透過 `attachByEdge` 函數生成**後續拼接的新圖形**時,回傳的物件屬性名稱卻寫成了 `shapeType`:
```javascript
return { vertices: worldVerts, rotation: foundK, mirrored, shapeType: templateKey };
```
3. 最後在繪製 SVG 的迴圈中,你統一呼叫了 `shape.type`:
```javascript
const fill = colors[shape.type][shape.mirrored];
```
因為除了第一個圖形之外,其他圖形都沒有 `type` 這個屬性(它們只有 `shapeType`),所以 `shape.type` 會回傳 `undefined`。當程式試圖讀取 `colors[undefined][false]` 時,就會拋出你看到的那個報錯:`can't access property "false", colors[shape.type] is undefined`。
---
### **修復方式**
你只需要把 `attachByEdge` 函數回傳物件裡的 `shapeType` 改成 `type`,讓所有圖形的資料結構保持一致即可。
請找到程式碼約第 253 行的 `attachByEdge` 函數結尾,並進行以下修改:
**修改前:**
```javascript
function attachByEdge(templateKey, mirrored, edgeIndexToAlign, worldP, worldQ) {
// ... 前面省略 ...
const worldVerts = localVerts.map(v => ringAdd(worldQ, ringMul(ringSub(v, localVerts[i]), zetaPow(foundK))));
return { vertices: worldVerts, rotation: foundK, mirrored, shapeType: templateKey };
}
```
**修改後:**
```javascript
function attachByEdge(templateKey, mirrored, edgeIndexToAlign, worldP, worldQ) {
// ... 前面省略 ...
const worldVerts = localVerts.map(v => ringAdd(worldQ, ringMul(ringSub(v, localVerts[i]), zetaPow(foundK))));
return { vertices: worldVerts, rotation: foundK, mirrored, type: templateKey }; // 將 shapeType 改為 type
}
```
存檔並重新整理頁面後,密鋪圖形應該就能夠順利生成了!https://ejsoon.vip/
弈趣極光:享受思維樂趣
弈趣極光:享受思維樂趣
Re: 一個新的遊戲創意
如何分別在這三個圖形內,畫出面積最大的圓,給出計算過程,以及圓心相對左下角的坐標。
gemini 3.6 flash
gemini 3.6 flash
代码: 全选
這三個圖形均由黃金比例 $\varphi = \frac{1+\sqrt{5}}{2} \approx 1.618$ 與特定角度($36^\circ, 72^\circ, 108^\circ, 144^\circ$)構成。以**左下角頂點為原點 $(0,0)$**、底邊落在正 $x$ 軸方向建立直角坐標系,計算過程與結果如下:
---
## 1. 藍色四邊形 (Figure 1)
### **幾何分析與頂點坐標**
* **頂點坐標**:
* $A = (0, 0)$
* $D = (2\varphi, 0) = (1+\sqrt{5}, 0) \approx (3.236, 0)$
* $B = (\varphi^2 \cos 36^\circ, \varphi^2 \sin 36^\circ) = \left(\frac{2+\sqrt{5}}{2}, \frac{\sqrt{10-2\sqrt{5}}(1+\sqrt{5})}{8}\right) \approx (2.118, 1.539)$
* $C = \left(\frac{5+3\sqrt{5}}{4}, \frac{\sqrt{10-2\sqrt{5}}}{4}\right) \approx (2.927, 0.951)$
### **最大內切圓計算**
此凸四邊形四邊無法同時切於同一個圓。最大圓為同時切於右下三條邊 $BC, CD, AD$ 的圓(即角 $C$ 與角 $D$ 的內角平分線交點):
1. **角 $D$ 平分線方程**:過 $D(2\varphi, 0)$,傾角為 $144^\circ$,直線為 $y = -\tan 36^\circ (x - 2\varphi)$。
2. **角 $C$ 平分線方程**:過 $C$,傾角為 $216^\circ$(斜率為 $\tan 36^\circ$),直線為 $y - y_C = \tan 36^\circ (x - x_C)$。
3. **求解圓心 $O_1(x_1, y_1)$**:
$$x_1 = \frac{3\varphi}{2} = \frac{3 + 3\sqrt{5}}{4} \approx 2.427$$
$$y_1 = R_1 = \sin 36^\circ = \frac{\sqrt{10-2\sqrt{5}}}{4} \approx 0.588$$
* **圓心坐標**:$\left(\frac{3+3\sqrt{5}}{4}, \frac{\sqrt{10-2\sqrt{5}}}{4}\right) \approx (2.427, 0.588)$
* **最大半徑**:$R_1 = \sin 36^\circ = \frac{\sqrt{10-2\sqrt{5}}}{4} \approx 0.588$
* **最大面積**:$S_1 = \pi R_1^2 = \frac{5-\sqrt{5}}{8}\pi \approx 1.086$
---
## 2. 橙色梯形 (Figure 2)
### **幾何分析與頂點坐標**
* 上底 $BC = 1$ 與下底 $AD = \varphi^2$ 互相平行(因 $\angle A + \angle B = 36^\circ + 144^\circ = 180^\circ$)。
* 梯形的高 $h = \varphi \sin 36^\circ = \sin 72^\circ = \frac{\sqrt{10+2\sqrt{5}}}{4} \approx 0.951$。
### **最大內切圓計算**
受限於兩平行邊 $BC$ 與 $AD$ 的距離,圓的最大直徑即為梯形的高 $h$,故最大半徑為:
$$R_2 = \frac{h}{2} = \frac{\varphi \sin 36^\circ}{2} = \frac{\sqrt{10+2\sqrt{5}}}{8} \approx 0.476$$
將圓放置於同時切於斜邊 $AB$、上底 $BC$ 與下底 $AD$ 的位置時,圓心位於角 $A$ 的平分線(傾角 $18^\circ$)上:
1. $y_2 = R_2 = \frac{\varphi \sin 36^\circ}{2} \approx 0.476$
2. $x_2 = \frac{y_2}{\tan 18^\circ} = \frac{3\varphi + 1}{4} = \frac{5 + 3\sqrt{5}}{8} \approx 1.464$
* **圓心坐標**:$\left(\frac{5+3\sqrt{5}}{8}, \frac{\sqrt{10+2\sqrt{5}}}{8}\right) \approx (1.464, 0.476)$
* **最大半徑**:$R_2 = \frac{\sqrt{10+2\sqrt{5}}}{8} \approx 0.476$
* **最大面積**:$S_2 = \pi R_2^2 = \frac{5+\sqrt{5}}{32}\pi \approx 0.710$
---
## 3. 綠色四邊形 (Figure 3)
### **幾何分析與頂點坐標**
* 邊 $AB$(長度 2,傾角 $36^\circ$)與邊 $CD$(長度 1,傾角 $36^\circ$)為一組**平行線**。
* 兩平行邊之間的垂直距離為 $H = \varphi \sin 36^\circ = \sin 72^\circ \approx 0.951$。
### **最大內切圓計算**
受限於平行邊 $AB$ 與 $CD$ 的間距,最大圓直徑等於 $H$,半徑為:
$$R_3 = \frac{H}{2} = \frac{\varphi \sin 36^\circ}{2} = \frac{\sqrt{10+2\sqrt{5}}}{8} \approx 0.476$$
令圓同時相切於平行邊 $AB, CD$ 以及底邊 $AD$(即 $y=0$):
1. $y_3 = R_3 = \frac{\varphi \sin 36^\circ}{2} \approx 0.476$
2. 圓心需落在兩平行邊的中線 $x \sin 36^\circ - y \cos 36^\circ = \frac{\varphi \sin 36^\circ}{2}$ 上,代入 $y_3$ 解得:
$$x_3 = \frac{\varphi^3}{4} = \frac{2+\sqrt{5}}{4} \approx 1.059$$
* **圓心坐標**:$\left(\frac{2+\sqrt{5}}{4}, \frac{\sqrt{10+2\sqrt{5}}}{8}\right) \approx (1.059, 0.476)$
* **最大半徑**:$R_3 = \frac{\sqrt{10+2\sqrt{5}}}{8} \approx 0.476$
* **最大面積**:$S_3 = \pi R_3^2 = \frac{5+\sqrt{5}}{32}\pi \approx 0.710$
---
## 📊 總結對比表
| 圖形 | 圓心相對左下角坐標 $(x, y)$ | 最大半徑 $R$ | 最大圓面積 $S$ |
| --- | --- | --- | --- |
| **圖 1 (藍色)** | $(2.427, 0.588)$ | $\sin 36^\circ \approx 0.588$ | $\approx 1.086$ |
| **圖 2 (橙色)** | $(1.464, 0.476)$ | $\frac{\sin 72^\circ}{2} \approx 0.476$ | $\approx 0.710$ |
| **圖 3 (綠色)** | $(1.059, 0.476)$ | $\frac{\sin 72^\circ}{2} \approx 0.476$ | $\approx 0.710$ |https://ejsoon.vip/
弈趣極光:享受思維樂趣
弈趣極光:享受思維樂趣
-
- 相似主题
- 回复总数
- 阅读次数
- 最新帖子
在线用户
正浏览此版面之用户: 没有注册用户 和 321 访客