Skip to content

Commit 3c88067

Browse files
committed
feat: add option to draw labels for angles and arcs
1 parent 323baaa commit 3c88067

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/components/Angle.vue

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<template>
2-
<path
3-
v-if="fill"
4-
:d="`M ${scaledB.x} ${scaledB.y} L ${start.x} ${start.y} A ${scaledRadius} ${scaledRadius} 0 ${sweep} 0 ${end.x} ${end.y}`"
5-
stroke="none"
6-
:fill="fill"
7-
/>
2+
<g>
3+
<path
4+
v-if="fill"
5+
:d="`M ${scaledB.x} ${scaledB.y} L ${start.x} ${start.y} A ${scaledRadius} ${scaledRadius} 0 ${sweep} 0 ${end.x} ${end.y}`"
6+
stroke="none"
7+
:fill="fill"
8+
/>
89

9-
<path
10-
:d="`M ${start.x} ${start.y} A ${scaledRadius} ${scaledRadius} 0 ${sweep} 0 ${end.x} ${end.y}`"
11-
:stroke-width="lineWidth * invScale"
12-
:stroke="stroke"
13-
:stroke-dasharray="dashArray"
14-
fill="none"
15-
/>
10+
<path
11+
:d="`M ${start.x} ${start.y} A ${scaledRadius} ${scaledRadius} 0 ${sweep} 0 ${end.x} ${end.y}`"
12+
:stroke-width="lineWidth * invScale"
13+
:stroke="stroke"
14+
:stroke-dasharray="dashArray"
15+
fill="none"
16+
/>
17+
18+
<Label
19+
v-if="label"
20+
:text="label"
21+
:position="labelPosition"
22+
:color="color"
23+
:size="labelSize"
24+
/>
25+
</g>
1626
</template>
1727

1828
<script setup lang="ts">
@@ -22,6 +32,8 @@ import { PossibleVector2, Vector2 } from "../utils/Vector2.ts";
2232
import { useGraphContext } from "../composables/useGraphContext.ts";
2333
import { Color } from "../types.ts";
2434
import { useColors } from "../composables/useColors.ts";
35+
import Label from "./Label.vue";
36+
import { TAU } from "../utils/constants.ts";
2537
2638
const props = withDefaults(
2739
defineProps<{
@@ -33,12 +45,15 @@ const props = withDefaults(
3345
c: PossibleVector2;
3446
dashed?: boolean;
3547
lineWidth?: number;
48+
label?: string;
49+
labelSize?: "small" | "normal" | "large";
3650
}>(),
3751
{
3852
position: () => new Vector2(),
3953
radius: 1,
4054
dashed: false,
4155
lineWidth: 1.5,
56+
labelSize: "small",
4257
},
4358
);
4459
@@ -81,4 +96,16 @@ const end = computed(() => {
8196
const dashArray = computed(() =>
8297
props.dashed ? [6 * invScale.value, 4 * invScale.value].join(",") : "0,0",
8398
);
99+
const labelPosition = computed(() => {
100+
const bToA = a.value.sub(b.value);
101+
const bToC = c.value.sub(b.value);
102+
const dot = bToA.dot(bToC);
103+
const det = bToA.x * bToC.y - bToA.y * bToC.x;
104+
const angle = (Math.atan2(det, dot) + TAU) % TAU;
105+
return bToA
106+
.normalized()
107+
.scale(props.radius)
108+
.rotate(angle / 2)
109+
.add(b.value);
110+
});
84111
</script>

src/components/Arc.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
:dashed="dashed"
88
:line-width="lineWidth"
99
:color="color"
10+
:label="label"
11+
:label-size="labelSize"
1012
/>
1113
</template>
1214

@@ -28,6 +30,8 @@ const props = withDefaults(
2830
dashed?: boolean;
2931
lineWidth?: number;
3032
radians?: boolean;
33+
label?: string;
34+
labelSize?: "small" | "normal" | "large";
3135
}>(),
3236
{
3337
from: 0,
@@ -36,6 +40,7 @@ const props = withDefaults(
3640
radius: 3,
3741
lineWidth: 1.25,
3842
radians: false,
43+
labelSize: "small",
3944
},
4045
);
4146

0 commit comments

Comments
 (0)