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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ER图生成器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        .container {
            display: flex;
            height: 100vh;
        }
        .editor-panel {
            flex: 0 0 40%;
            padding: 20px;
            background-color: white;
            border-right: 1px solid #ddd;
            display: flex;
            flex-direction: column;
            overflow: auto;
        }
        .preview-panel {
            flex: 0 0 60%;
            background-color: white;
            overflow: auto;
        }
        h1, h2, h3 {
            color: #333;
            margin-top: 0;
        }
        .title-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        textarea {
            flex: 1;
            padding: 10px;
            font-family: monospace;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: none;
            min-height: 300px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 15px;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .syntax-guide {
            margin-top: 20px;
            background-color: #f9f9f9;
            padding: 15px;
            border-radius: 4px;
            font-size: 14px;
        }
        pre {
            white-space: pre-wrap;
            margin: 0;
            font-family: monospace;
            font-size: 14px;
        }
        input[type="text"] {
            padding: 8px 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            width: 300px;
        }
        .preview-controls {
            padding: 20px;
            border-bottom: 1px solid #eee;
        }
        #svgContainer {
            padding: 20px;
        }
        svg {
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="editor-panel">
            <div class="title-bar">
                <h2>ER图代码编辑器</h2>
            </div>
            <textarea id="erCode" rows="20">// 实体定义
entity: 员工
  pk: 员工编号
  attr: 姓名
  attr: 性别
  attr: 出生日期
  attr: 电话

entity: 部门
  pk: 部门编号
  attr: 部门名称
  attr: 位置

entity: 项目
  pk: 项目编号
  attr: 项目名称
  attr: 开始日期
  attr: 结束日期
  attr: 预算

entity: 客户
  pk: 客户编号
  attr: 客户名称
  attr: 联系人
  attr: 电话

relation: 隶属
  from: 员工 (N)
  to: 部门 (1)

relation: 管理
  from: 员工 (1)
  to: 项目 (N)

relation: 参与
  from: 员工 (N)
  to: 项目 (N)

relation: 合作
  from: 项目 (N)
  to: 客户 (1)</textarea>
            <button id="generateBtn">生成ER图</button>
            <div class="syntax-guide">
                <h3>语法说明:</h3>
                <pre>// 实体定义
entity: 实体名
  pk: 主键字段
  attr: 属性1
  attr: 属性2

// 关系定义
relation: 关系名
  from: 实体1 (基数)
  to: 实体2 (基数)

// 基数表示: 1, N, M等</pre>
            </div>
        </div>
        <div class="preview-panel">
            <div class="preview-controls">
                <div class="title-bar">
                    <h2>ER图预览</h2>
                    <div>
                        <span>标题: </span>
                        <input type="text" id="diagramTitle" value="企业人事管理系统">
                    </div>
                </div>
            </div>
            <div id="svgContainer"></div>
        </div>
    </div>

    <script>
        // 解析ER图代码
        function parseERCode(code) {
            const entities = [];
            const relations = [];
            
            let currentEntity = null;
            let currentRelation = null;
            
            const lines = code.split('\n');
            
            for (const line of lines) {
                const trimmedLine = line.trim();
                if (trimmedLine === '' || trimmedLine.startsWith('//')) continue;
                
                // 实体定义
                if (trimmedLine.startsWith('entity:')) {
                    if (currentEntity) entities.push(currentEntity);
                    if (currentRelation) relations.push(currentRelation);
                    
                    currentEntity = {
                        name: trimmedLine.substring(7).trim(),
                        pk: null,
                        attributes: []
                    };
                    currentRelation = null;
                }
                // 主键定义
                else if (trimmedLine.startsWith('pk:') && currentEntity) {
                    currentEntity.pk = trimmedLine.substring(3).trim();
                }
                // 属性定义
                else if (trimmedLine.startsWith('attr:') && currentEntity) {
                    currentEntity.attributes.push(trimmedLine.substring(5).trim());
                }
                // 关系定义
                else if (trimmedLine.startsWith('relation:')) {
                    if (currentEntity) entities.push(currentEntity);
                    if (currentRelation) relations.push(currentRelation);
                    
                    currentEntity = null;
                    currentRelation = {
                        name: trimmedLine.substring(9).trim(),
                        from: null,
                        to: null
                    };
                }
                // 关系源
                else if (trimmedLine.startsWith('from:') && currentRelation) {
                    const match = trimmedLine.match(/from:\s+([^\(]+)\s+\(([^\)]+)\)/);
                    if (match) {
                        currentRelation.from = {
                            entity: match[1].trim(),
                            cardinality: match[2].trim()
                        };
                    }
                }
                // 关系目标
                else if (trimmedLine.startsWith('to:') && currentRelation) {
                    const match = trimmedLine.match(/to:\s+([^\(]+)\s+\(([^\)]+)\)/);
                    if (match) {
                        currentRelation.to = {
                            entity: match[1].trim(),
                            cardinality: match[2].trim()
                        };
                    }
                }
            }
            
            // 添加最后一个实体或关系
            if (currentEntity) entities.push(currentEntity);
            if (currentRelation) relations.push(currentRelation);
            
            return { entities, relations };
        }
        
        // 创建ER图布局
        function createLayout(entities, relations) {
            // 基础画布尺寸设置
            const minWidth = 1200;
            const minHeight = 900;
            const width = Math.max(minWidth, entities.length * 300);
            const height = Math.max(minHeight, entities.length * 250);
            
            const layout = {
                width: width,
                height: height,
                entities: {},
                relations: {}
            };
            
            // 实体位置分配 - 改进的分区策略
            if (entities.length <= 4) {
                // 使用更合理的四角布局
                const positions = [
                    { x: width / 2, y: height * 0.25 },  // 上
                    { x: width * 0.25, y: height / 2 },  // 左
                    { x: width * 0.75, y: height / 2 },  // 右
                    { x: width / 2, y: height * 0.75 }   // 下
                ];
                
                entities.forEach((entity, index) => {
                    layout.entities[entity.name] = {
                        x: positions[index].x,
                        y: positions[index].y,
                        width: 120,
                        height: 60,
                        attributes: [],
                        section: index // 记录实体所在区域(上/左/右/下)
                    };
                });
            } else {
                // 多实体情况下的均匀分布
                const centerX = width / 2;
                const centerY = height / 2;
                const radius = Math.min(width, height) * 0.3;
                
                entities.forEach((entity, index) => {
                    const angle = (index * 2 * Math.PI) / entities.length;
                    const x = centerX + radius * Math.cos(angle);
                    const y = centerY + radius * Math.sin(angle);
                    
                    // 确定区域象限
                    let section;
                    if (y < centerY && Math.abs(x - centerX) < radius * 0.5) section = 0; // 上
                    else if (x < centerX && Math.abs(y - centerY) < radius * 0.5) section = 1; // 左
                    else if (x > centerX && Math.abs(y - centerY) < radius * 0.5) section = 2; // 右
                    else section = 3; // 下
                    
                    layout.entities[entity.name] = {
                        x: x,
                        y: y,
                        width: 120,
                        height: 60,
                        attributes: [],
                        section: section
                    };
                });
            }
            
            // 属性布局优化 - 根据实体区域确定属性位置
            const occupiedSpaces = []; // 记录所有已占用的空间
            
            // 先添加所有实体占用的空间
            for (const [entityName, entityLayout] of Object.entries(layout.entities)) {
                occupiedSpaces.push({
                    x: entityLayout.x,
                    y: entityLayout.y,
                    width: entityLayout.width + 20, // 额外边距
                    height: entityLayout.height + 20,
                    type: 'entity'
                });
            }
            
            // 为每个实体分配属性空间
            for (const [entityName, entityLayout] of Object.entries(layout.entities)) {
                const entity = entities.find(e => e.name === entityName);
                if (!entity) continue;
                
                // 收集属性
                const allAttributes = [];
                if (entity.pk) {
                    allAttributes.push({ name: entity.pk, isPk: true });
                }
                
                entity.attributes.forEach(attr => {
                    allAttributes.push({ name: attr, isPk: false });
                });
                
                // 根据实体区域确定属性首选方向
                let preferredDirections = [];
                
                switch(entityLayout.section) {
                    case 0: // 上方实体
                        preferredDirections = ['top', 'left', 'right', 'bottom'];
                        break;
                    case 1: // 左侧实体
                        preferredDirections = ['left', 'top', 'bottom', 'right'];
                        break;
                    case 2: // 右侧实体
                        preferredDirections = ['right', 'top', 'bottom', 'left'];
                        break;
                    case 3: // 下方实体
                        preferredDirections = ['bottom', 'left', 'right', 'top'];
                        break;
                }
                
                // 为主键找最优位置(首选方向)
                let pkIndex = allAttributes.findIndex(attr => attr.isPk);
                if (pkIndex >= 0) {
                    const pk = allAttributes[pkIndex];
                    const bestDirection = findBestDirection(entityLayout, pk, preferredDirections[0], occupiedSpaces);
                    
                    // 放置主键
                    const pkPosition = positionInDirection(entityLayout, bestDirection, 120);
                    
                    entityLayout.pk = {
                        x: pkPosition.x,
                        y: pkPosition.y,
                        width: 60,
                        height: 35,
                        name: pk.name
                    };
                    
                    // 添加到已占用空间
                    occupiedSpaces.push({
                        x: pkPosition.x,
                        y: pkPosition.y,
                        width: 130, // 为了避免重叠增加了空间
                        height: 80,
                        type: 'pk'
                    });
                    
                    // 从属性列表移除
                    allAttributes.splice(pkIndex, 1);
                }
                
                // 为普通属性找位置(尽量按首选方向,但可调整避免重叠)
                allAttributes.forEach((attr, i) => {
                    // 尝试每个方向直到找到合适位置
                    let bestDirection = null;
                    let bestPosition = null;
                    
                    // 尝试所有方向
                    for (const direction of preferredDirections) {
                        // 生成候选位置(带偏移)
                        for (let offset = 0; offset <= 4; offset++) {
                            const distance = 120 + offset * 20; // 逐渐增加距离
                            const angleOffset = (offset * 0.1) * (i % 2 === 0 ? 1 : -1); // 左右交替偏移
                            
                            const position = positionInDirection(entityLayout, direction, distance, angleOffset);
                            
                            // 检查是否与已占用空间重叠
                            if (!hasOverlap(position, occupiedSpaces)) {
                                bestDirection = direction;
                                bestPosition = position;
                                break;
                            }
                        }
                        
                        if (bestPosition) break; // 找到了就停止寻找
                    }
                    
                    // 如果没找到无重叠位置,取最后一个方向的位置并强制增加距离
                    if (!bestPosition) {
                        const lastDirection = preferredDirections[preferredDirections.length - 1];
                        bestPosition = positionInDirection(entityLayout, lastDirection, 200 + i * 30);
                    }
                    
                    // 放置属性
                    const attrObj = {
                        x: bestPosition.x,
                        y: bestPosition.y,
                        width: 55,
                        height: 30,
                        name: attr.name
                    };
                    
                    entityLayout.attributes.push(attrObj);
                    
                    // 添加到已占用空间
                    occupiedSpaces.push({
                        x: bestPosition.x,
                        y: bestPosition.y,
                        width: 120,
                        height: 70,
                        type: 'attr'
                    });
                });
            }
            
            // 关系位置优化 - 动态调整关系菱形位置
            relations.forEach(relation => {
                const fromEntity = layout.entities[relation.from.entity];
                const toEntity = layout.entities[relation.to.entity];
                
                if (fromEntity && toEntity) {
                    // 计算两实体中点
                    const dx = toEntity.x - fromEntity.x;
                    const dy = toEntity.y - fromEntity.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    // 动态调整居中比例 - 距离越远比例越接近0.5
                    const ratio = 0.4 + (distance / 1500); // 调整系数使布局更紧凑
                    const midX = fromEntity.x + dx * ratio;
                    const midY = fromEntity.y + dy * ratio;
                    
                    // 寻找无重叠位置
                    let bestX = midX;
                    let bestY = midY;
                    let minOverlaps = Number.MAX_VALUE;
                    
                    // 在中点附近探索多个位置
                    for (let offsetX = -60; offsetX <= 60; offsetX += 20) {
                        for (let offsetY = -60; offsetY <= 60; offsetY += 20) {
                            const testX = midX + offsetX;
                            const testY = midY + offsetY;
                            
                            // 计算此位置与已有组件的重叠数
                            let overlaps = 0;
                            for (const space of occupiedSpaces) {
                                if (isPointInRect(testX, testY, space)) {
                                    overlaps++;
                                }
                            }
                            
                            // 如果有更少重叠,更新最佳位置
                            if (overlaps < minOverlaps) {
                                minOverlaps = overlaps;
                                bestX = testX;
                                bestY = testY;
                                
                                // 如果没有重叠,立即使用
                                if (overlaps === 0) break;
                            }
                        }
                        if (minOverlaps === 0) break;
                    }
                    
                    // 如果还是有重叠,再增加搜索范围
                    if (minOverlaps > 0) {
                        const perpX = -dy / distance * 100; // 垂直于连线方向
                        const perpY = dx / distance * 100;
                        
                        // 尝试垂直偏移
                        bestX = midX + perpX;
                        bestY = midY + perpY;
                        
                        // 检查是否仍有重叠
                        let stillOverlap = false;
                        for (const space of occupiedSpaces) {
                            if (isPointInRect(bestX, bestY, space)) {
                                stillOverlap = true;
                                break;
                            }
                        }
                        
                        // 如有必要,尝试反方向
                        if (stillOverlap) {
                            bestX = midX - perpX;
                            bestY = midY - perpY;
                        }
                    }
                    
                    // 保存最终关系位置
                    layout.relations[relation.name] = {
                        x: bestX,
                        y: bestY,
                        width: 45,
                        height: 25,
                        from: relation.from,
                        to: relation.to
                    };
                    
                    // 添加到已占用空间
                    occupiedSpaces.push({
                        x: bestX,
                        y: bestY,
                        width: 100,
                        height: 60,
                        type: 'relation'
                    });
                }
            });
            
            return layout;
        }
        
        // 辅助函数: 检查位置是否与已占用空间重叠
        function hasOverlap(position, occupiedSpaces) {
            const testRect = {
                x: position.x,
                y: position.y,
                width: 120, // 属性空间
                height: 70
            };
            
            for (const space of occupiedSpaces) {
                if (doRectsOverlap(testRect, space)) {
                    return true;
                }
            }
            
            return false;
        }
        
        // 矩形重叠检测
        function doRectsOverlap(rect1, rect2) {
            const minDistance = (rect1.width + rect2.width) / 2 * 0.8; // 80%宽度作为水平安全距离
            const minVertical = (rect1.height + rect2.height) / 2 * 0.8; // 80%高度作为垂直安全距离
            
            const dx = Math.abs(rect1.x - rect2.x);
            const dy = Math.abs(rect1.y - rect2.y);
            
            return dx < minDistance && dy < minVertical;
        }
        
        // 检查点是否在矩形内(或距离过近)
        function isPointInRect(x, y, rect) {
            const halfWidth = rect.width / 2;
            const halfHeight = rect.height / 2;
            
            return Math.abs(x - rect.x) < halfWidth && 
                   Math.abs(y - rect.y) < halfHeight;
        }
        
        // 在指定方向上找位置(带角度偏移)
        function positionInDirection(entityLayout, direction, distance, angleOffset = 0) {
            let angle;
            
            switch(direction) {
                case 'top':
                    angle = -Math.PI/2 + (angleOffset || 0);
                    break;
                case 'right':
                    angle = 0 + (angleOffset || 0);
                    break;
                case 'bottom':
                    angle = Math.PI/2 + (angleOffset || 0);
                    break;
                case 'left':
                    angle = Math.PI + (angleOffset || 0);
                    break;
            }
            
            return {
                x: entityLayout.x + Math.cos(angle) * distance,
                y: entityLayout.y + Math.sin(angle) * distance
            };
        }
        
        // 找到最佳方向(最少重叠)
        function findBestDirection(entityLayout, attr, preferredDirection, occupiedSpaces) {
            const directions = ['top', 'right', 'bottom', 'left'];
            let bestDirection = preferredDirection;
            let minOverlaps = Number.MAX_VALUE;
            
            // 尝试所有方向
            for (const direction of directions) {
                const position = positionInDirection(entityLayout, direction, 120);
                
                // 计算此方向的重叠
                let overlaps = 0;
                for (const space of occupiedSpaces) {
                    if (isPointInRect(position.x, position.y, space)) {
                        overlaps++;
                    }
                }
                
                // 优先考虑首选方向,但也要权衡重叠情况
                const directionPenalty = (direction === preferredDirection) ? 0 : 1;
                const score = overlaps + directionPenalty;
                
                if (score < minOverlaps) {
                    minOverlaps = score;
                    bestDirection = direction;
                }
            }
            
            return bestDirection;
        }
        
        // 计算实体与关系之间的连接点
        function calculateConnectionPoint(entity, relation) {
            const dx = relation.x - entity.x;
            const dy = relation.y - entity.y;
            const angle = Math.atan2(dy, dx);
            
            // 计算矩形边界上的交点
            let intersectX, intersectY;
            
            // 计算水平和垂直方向的交点距离
            const width = entity.width / 2;
            const height = entity.height / 2;
            
            // 处理特殊情况 - 垂直或水平线
            if (Math.abs(dx) < 0.001) { // 垂直线
                intersectX = entity.x;
                intersectY = entity.y + Math.sign(dy) * height;
            } else if (Math.abs(dy) < 0.001) { // 水平线
                intersectX = entity.x + Math.sign(dx) * width;
                intersectY = entity.y;
            } else {
                // 一般情况 - 计算与矩形边界的交点
                const slope = dy / dx;
                const invSlope = dx / dy;
                
                // 检查与垂直边界的交点
                const vertX = entity.x + Math.sign(dx) * width;
                const vertY = entity.y + slope * (vertX - entity.x);
                
                // 检查与水平边界的交点
                const horizY = entity.y + Math.sign(dy) * height;
                const horizX = entity.x + invSlope * (horizY - entity.y);
                
                // 选择最接近实体中心的交点
                const dxVert = Math.abs(vertX - entity.x);
                const dyVert = Math.abs(vertY - entity.y);
                const dVert = Math.sqrt(dxVert * dxVert + dyVert * dyVert);
                
                const dxHoriz = Math.abs(horizX - entity.x);
                const dyHoriz = Math.abs(horizY - entity.y);
                const dHoriz = Math.sqrt(dxHoriz * dxHoriz + dyHoriz * dyHoriz);
                
                if (dVert <= dHoriz && Math.abs(vertY - entity.y) <= height) {
                    intersectX = vertX;
                    intersectY = vertY;
                } else if (Math.abs(horizX - entity.x) <= width) {
                    intersectX = horizX;
                    intersectY = horizY;
                } else {
                    // 边缘情况 - 使用角点
                    intersectX = entity.x + Math.sign(dx) * width;
                    intersectY = entity.y + Math.sign(dy) * height;
                }
            }
            
            return { x: intersectX, y: intersectY };
        }
        
        // 计算实体到属性的连接点
        function calculateEntityToAttributeConnection(entity, attribute) {
            const dx = attribute.x - entity.x;
            const dy = attribute.y - entity.y;
            const angle = Math.atan2(dy, dx);
            
            // 同样的方法,但简化版本
            const width = entity.width / 2;
            const height = entity.height / 2;
            
            // 考虑特殊情况
            if (Math.abs(dx) < 0.001) { // 垂直线
                return {
                    x: entity.x,
                    y: entity.y + Math.sign(dy) * height
                };
            } else if (Math.abs(dy) < 0.001) { // 水平线
                return {
                    x: entity.x + Math.sign(dx) * width,
                    y: entity.y
                };
            } else {
                // 一般情况
                const slope = dy / dx;
                
                // 比较水平和垂直交点
                const tx = width / Math.abs(Math.cos(angle));
                const ty = height / Math.abs(Math.sin(angle));
                
                if (tx < ty) {
                    return {
                        x: entity.x + Math.sign(dx) * width,
                        y: entity.y + slope * Math.sign(dx) * width
                    };
                } else {
                    return {
                        x: entity.x + Math.sign(dy) * height / slope,
                        y: entity.y + Math.sign(dy) * height
                    };
                }
            }
        }
        
        // 计算属性到实体的连接点 (用于椭圆)
        function calculateAttributeToEntityConnection(attribute, entity) {
            const dx = entity.x - attribute.x;
            const dy = entity.y - attribute.y;
            const angle = Math.atan2(dy, dx);
            
            // 椭圆上的点
            return {
                x: attribute.x + Math.cos(angle) * attribute.width,
                y: attribute.y + Math.sin(angle) * attribute.height
            };
        }
        
        // 生成SVG
        function generateSVG(entities, relations, layout, title) {
            // 首先准备SVG容器
            let svgContent = `
            <svg width="${layout.width}" height="${layout.height}" xmlns="http://www.w3.org/2000/svg">
            `;
            
            // 添加标题
            svgContent += `
                <text x="${layout.width/2}" y="40" text-anchor="middle" font-size="24" font-weight="bold" fill="#333">${title}</text>
            `;
            
            // 创建分层SVG - 确保正确的Z顺序
            // 1. 底层:所有连接线
            let linesLayer = '<g id="lines-layer">\n';
            
            // 2. 实体-关系连接线
            for (const [relationName, relationLayout] of Object.entries(layout.relations)) {
                const fromEntity = layout.entities[relationLayout.from.entity];
                const toEntity = layout.entities[relationLayout.to.entity];
                
                if (fromEntity && toEntity) {
                    // 计算精确的连接点
                    const fromConnection = calculateConnectionPoint(fromEntity, relationLayout);
                    const toConnection = calculateConnectionPoint(toEntity, relationLayout);
                    
                    // 实体-关系连接线
                    linesLayer += `
                        <line x1="${fromConnection.x}" y1="${fromConnection.y}" x2="${relationLayout.x}" y2="${relationLayout.y}" 
                              stroke="#6c757d" stroke-width="2" />
                        <line x1="${relationLayout.x}" y1="${relationLayout.y}" x2="${toConnection.x}" y2="${toConnection.y}" 
                              stroke="#6c757d" stroke-width="2" />
                    `;
                    
                    // 基数标记位置 - 距离实体连接点1/4处
                    const fromCardX = fromConnection.x + (relationLayout.x - fromConnection.x) * 0.25;
                    const fromCardY = fromConnection.y + (relationLayout.y - fromConnection.y) * 0.25;
                    
                    const toCardX = toConnection.x + (relationLayout.x - toConnection.x) * 0.25;
                    const toCardY = toConnection.y + (relationLayout.y - toConnection.y) * 0.25;
                    
                    // 基数文本
                    linesLayer += `
                        <text x="${fromCardX}" y="${fromCardY}" text-anchor="middle" dominant-baseline="middle" 
                              font-weight="bold" font-size="16" fill="#dc3545">${relationLayout.from.cardinality}</text>
                        <text x="${toCardX}" y="${toCardY}" text-anchor="middle" dominant-baseline="middle" 
                              font-weight="bold" font-size="16" fill="#dc3545">${relationLayout.to.cardinality}</text>
                    `;
                }
            }
            
            // 实体-属性连接线
            for (const [entityName, entityLayout] of Object.entries(layout.entities)) {
                // 主键连接线
                if (entityLayout.pk) {
                    const pk = entityLayout.pk;
                    
                    // 计算精确的连接点
                    const entityConnection = calculateEntityToAttributeConnection(entityLayout, pk);
                    const pkConnection = calculateAttributeToEntityConnection(pk, entityLayout);
                    
                    linesLayer += `
                        <line x1="${entityConnection.x}" y1="${entityConnection.y}" x2="${pkConnection.x}" y2="${pkConnection.y}" 
                              stroke="#dc3545" stroke-width="1.5" />
                    `;
                }
                
                // 普通属性连接线
                entityLayout.attributes.forEach(attr => {
                    // 计算精确的连接点
                    const entityConnection = calculateEntityToAttributeConnection(entityLayout, attr);
                    const attrConnection = calculateAttributeToEntityConnection(attr, entityLayout);
                    
                    linesLayer += `
                        <line x1="${entityConnection.x}" y1="${entityConnection.y}" x2="${attrConnection.x}" y2="${attrConnection.y}" 
                              stroke="#28a745" stroke-width="1.5" />
                    `;
                });
            }
            
            linesLayer += '</g>\n';
            
            // 3. 中层:关系菱形
            let relationshipsLayer = '<g id="relationships-layer">\n';
            
            for (const [relationName, relationLayout] of Object.entries(layout.relations)) {
                const x = relationLayout.x;
                const y = relationLayout.y;
                const w = relationLayout.width;
                const h = relationLayout.height;
                
                relationshipsLayer += `
                    <polygon points="${x},${y-h} ${x+w},${y} ${x},${y+h} ${x-w},${y}" 
                             fill="#fff3cd" stroke="#ffc107" stroke-width="1.5" />
                    <text x="${x}" y="${y}" text-anchor="middle" dominant-baseline="middle" 
                          font-size="14" fill="#212529">${relationName}</text>
                `;
            }
            
            relationshipsLayer += '</g>\n';
            
            // 4. 上层:实体矩形和属性椭圆
            let entitiesLayer = '<g id="entities-layer">\n';
            
            for (const [entityName, entityLayout] of Object.entries(layout.entities)) {
                // 绘制属性椭圆
                // 先画主键
                if (entityLayout.pk) {
                    const pk = entityLayout.pk;
                    entitiesLayer += `
                        <ellipse cx="${pk.x}" cy="${pk.y}" rx="${pk.width}" ry="${pk.height}" 
                                fill="#ffebee" stroke="#dc3545" stroke-width="2" />
                        <text x="${pk.x}" y="${pk.y}" text-anchor="middle" dominant-baseline="middle" 
                              font-size="14" font-weight="bold">${pk.name}</text>
                    `;
                }
                
                // 普通属性
                entityLayout.attributes.forEach(attr => {
                    entitiesLayer += `
                        <ellipse cx="${attr.x}" cy="${attr.y}" rx="${attr.width}" ry="${attr.height}" 
                                fill="#f8f9fa" stroke="#28a745" stroke-width="1.5" />
                        <text x="${attr.x}" y="${attr.y}" text-anchor="middle" dominant-baseline="middle" 
                              font-size="14">${attr.name}</text>
                    `;
                });
                
                // 绘制实体矩形
                entitiesLayer += `
                    <rect x="${entityLayout.x - entityLayout.width/2}" y="${entityLayout.y - entityLayout.height/2}" 
                          width="${entityLayout.width}" height="${entityLayout.height}" rx="5" ry="5"
                          fill="#e3f2fd" stroke="#0d6efd" stroke-width="2" />
                    <text x="${entityLayout.x}" y="${entityLayout.y}" text-anchor="middle" dominant-baseline="middle" 
                          font-size="16" font-weight="bold" fill="#0d6efd">${entityName}</text>
                `;
            }
            
            entitiesLayer += '</g>\n';
            
            // 组合所有层级,确保正确的Z轴顺序
            svgContent += linesLayer;    // 最底层:连接线
            svgContent += relationshipsLayer; // 中间层:关系菱形
            svgContent += entitiesLayer;  // 最上层:实体和属性
            
            svgContent += '</svg>';
            return svgContent;
        }
        
        // 生成ER图
        function generateERDiagram() {
            const erCode = document.getElementById('erCode').value;
            const title = document.getElementById('diagramTitle').value || 'ER图';
            
            try {
                const { entities, relations } = parseERCode(erCode);
                const layout = createLayout(entities, relations);
                const svgContent = generateSVG(entities, relations, layout, title);
                
                document.getElementById('svgContainer').innerHTML = svgContent;
            } catch (error) {
                console.error('生成ER图时出错:', error);
                alert('生成ER图时出错: ' + error.message);
            }
        }
        
        // 初始加载和事件监听
        document.addEventListener('DOMContentLoaded', function() {
            // 初始生成
            generateERDiagram();
            
            // 绑定生成按钮事件
            document.getElementById('generateBtn').addEventListener('click', generateERDiagram);
            
            // 绑定标题更改事件
            document.getElementById('diagramTitle').addEventListener('change', generateERDiagram);
        });
    </script>
</body>
</html>