-
Notifications
You must be signed in to change notification settings - Fork 49
/
PointRenderer.kt
181 lines (164 loc) · 6.08 KB
/
PointRenderer.kt
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
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
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.livemap.chart.point
import org.jetbrains.letsPlot.commons.intern.math.toRadians
import org.jetbrains.letsPlot.core.canvas.Context2d
import org.jetbrains.letsPlot.livemap.chart.ChartElementComponent
import org.jetbrains.letsPlot.livemap.chart.PointComponent
import org.jetbrains.letsPlot.livemap.core.ecs.EcsEntity
import org.jetbrains.letsPlot.livemap.mapengine.RenderHelper
import org.jetbrains.letsPlot.livemap.mapengine.Renderer
import org.jetbrains.letsPlot.livemap.mapengine.placement.WorldOriginComponent
import org.jetbrains.letsPlot.livemap.mapengine.translate
import kotlin.math.PI
import kotlin.math.sqrt
class PointRenderer(
private val shape: Int,
degreeAngle: Double
) : Renderer {
private val angle = toRadians(degreeAngle)
override fun render(entity: EcsEntity, ctx: Context2d, renderHelper: RenderHelper) {
val chartElement = entity.get<ChartElementComponent>()
val pointData = entity.get<PointComponent>()
ctx.translate(renderHelper.dimToScreen(entity.get<WorldOriginComponent>().origin))
ctx.beginPath()
drawMarker(
ctx = ctx,
radius = pointData.scaledRadius(chartElement.scalingSizeFactor),
stroke = chartElement.scaledStrokeWidth(),
shape = shape,
angle = angle
)
if (chartElement.fillColor != null) {
ctx.setFillStyle(chartElement.scaledFillColor())
ctx.fill()
}
if (chartElement.strokeColor != null && chartElement.scaledStrokeWidth() > 0.0) {
ctx.setStrokeStyle(chartElement.scaledStrokeColor())
ctx.setLineWidth(chartElement.scaledStrokeWidth())
ctx.stroke()
}
}
private fun drawMarker(ctx: Context2d, radius: Double, stroke: Double, shape: Int, angle: Double) {
val needToRotate = shape !in listOf(1, 10, 16, 19, 20, 21) && angle != 0.0
if (needToRotate) {
ctx.rotate(angle)
}
when (shape) {
0 -> square(ctx, radius)
1 -> circle(ctx, radius)
2 -> triangle(ctx, radius, stroke)
3 -> plus(ctx, radius)
4 -> cross(ctx, radius / sqrt(2.0))
5 -> diamond(ctx, radius)
6 -> triangle(ctx, radius, stroke, pointingUp = false)
7 -> {
square(ctx, radius)
cross(ctx, radius)
}
8 -> {
plus(ctx, radius)
cross(ctx, radius / sqrt(2.0))
}
9 -> {
diamond(ctx, radius)
plus(ctx, radius)
}
10 -> {
circle(ctx, radius)
plus(ctx, radius)
}
11 -> {
triangle(ctx, radius, stroke, pointingUp = true, pinnedToCentroid = true)
triangle(ctx, radius, stroke, pointingUp = false, pinnedToCentroid = true)
}
12 -> {
square(ctx, radius)
plus(ctx, radius)
}
13 -> {
circle(ctx, radius)
cross(ctx, radius / sqrt(2.0))
}
14 -> squareTriangle(ctx, radius, stroke)
15 -> square(ctx, radius)
16 -> circle(ctx, radius)
17 -> triangle(ctx, radius, 1.0)
18 -> diamond(ctx, radius)
19 -> circle(ctx, radius)
20 -> circle(ctx, radius)
21 -> circle(ctx, radius)
22 -> square(ctx, radius)
23 -> diamond(ctx, radius)
24 -> triangle(ctx, radius, stroke)
25 -> triangle(ctx, radius, stroke, pointingUp = false)
else -> throw IllegalStateException("Unknown point shape")
}
if (needToRotate) {
ctx.rotate(-angle)
}
}
private fun circle(ctx: Context2d, r: Double) {
ctx.arc(0.0, 0.0, r, 0.0, 2 * PI)
}
private fun square(ctx: Context2d, r: Double) {
ctx.moveTo(-r, -r)
ctx.lineTo(r, -r)
ctx.lineTo(r, r)
ctx.lineTo(-r, r)
ctx.closePath()
}
private fun squareTriangle(ctx: Context2d, r: Double, stroke: Double) {
val outerSize = 2 * r + stroke
val triangleHeight = outerSize - stroke / 2 - sqrt(5.0) * stroke / 2
ctx.moveTo(-triangleHeight / 2, r)
ctx.lineTo(0.0, r - triangleHeight)
ctx.lineTo(triangleHeight / 2, r)
ctx.lineTo(-r, r)
ctx.lineTo(-r, -r)
ctx.lineTo(r, -r)
ctx.lineTo(r, r)
ctx.closePath()
}
private fun triangle(ctx: Context2d, r: Double, stroke: Double, pointingUp: Boolean = true, pinnedToCentroid: Boolean = false) {
val outerHeight = 2 * r + stroke
val height = outerHeight - 3.0 * stroke / 2.0
val side = 2.0 * height / sqrt(3.0)
val distanceToBase = (outerHeight - stroke) / 2.0
val distanceToPeak = height - distanceToBase
val pointingCoeff = if (pointingUp)
1.0
else
-1.0
val centroidOffset = if (pinnedToCentroid)
height / 6.0 + stroke / 4.0
else
0.0
ctx.moveTo(0.0, -pointingCoeff * (distanceToPeak + centroidOffset))
ctx.lineTo(side / 2.0, pointingCoeff * (distanceToBase - centroidOffset))
ctx.lineTo(-side / 2.0, pointingCoeff * (distanceToBase - centroidOffset))
ctx.lineTo(0.0, -pointingCoeff * (distanceToPeak + centroidOffset))
ctx.closePath()
}
internal fun plus(ctx: Context2d, r: Double) {
ctx.moveTo(0.0, -r)
ctx.lineTo(0.0, r)
ctx.moveTo(-r, 0.0)
ctx.lineTo(r, 0.0)
}
private fun cross(ctx: Context2d, r: Double) {
ctx.moveTo(-r, -r)
ctx.lineTo(r, r)
ctx.moveTo(-r, r)
ctx.lineTo(r, -r)
}
private fun diamond(ctx: Context2d, r: Double) {
ctx.moveTo(0.0, -r)
ctx.lineTo(r, 0.0)
ctx.lineTo(0.0, r)
ctx.lineTo(-r, 0.0)
ctx.closePath()
}
}