1 /*
<lambda>null2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.egg.landroid
18
19 import android.util.Log
20 import androidx.compose.ui.graphics.Path
21 import kotlin.math.cos
22 import kotlin.math.sin
23
24 fun createPolygon(radius: Float, sides: Int): Path {
25 return Path().apply {
26 moveTo(radius, 0f)
27 val angleStep = PI2f / sides
28 for (i in 1 until sides) {
29 lineTo(radius * cos(angleStep * i), radius * sin(angleStep * i))
30 }
31 close()
32 }
33 }
34
createPolygonPointsnull35 fun createPolygonPoints(radius: Float, sides: Int): List<Vec2> {
36 val angleStep = PI2f / sides
37 return (0 until sides).map { i ->
38 Vec2(radius * cos(angleStep * i), radius * sin(angleStep * i))
39 }
40 }
41
createStarnull42 fun createStar(radius1: Float, radius2: Float, points: Int): Path {
43 return Path().apply {
44 val angleStep = PI2f / points
45 moveTo(radius1, 0f)
46 lineTo(radius2 * cos(angleStep * (0.5f)), radius2 * sin(angleStep * (0.5f)))
47 for (i in 1 until points) {
48 lineTo(radius1 * cos(angleStep * i), radius1 * sin(angleStep * i))
49 lineTo(radius2 * cos(angleStep * (i + 0.5f)), radius2 * sin(angleStep * (i + 0.5f)))
50 }
51 close()
52 }
53 }
54
Pathnull55 fun Path.parseSvgPathData(d: String) {
56 Regex("([A-Za-z])\\s*([-.,0-9e ]+)").findAll(d.trim()).forEach {
57 val cmd = it.groups[1]!!.value
58 val args =
59 it.groups[2]?.value?.split(Regex("\\s+"))?.map { v -> v.toFloat() } ?: emptyList()
60 // Log.d("Landroid", "cmd = $cmd, args = " + args.joinToString(","))
61 when (cmd) {
62 "M" -> moveTo(args[0], args[1])
63 "C" -> cubicTo(args[0], args[1], args[2], args[3], args[4], args[5])
64 "L" -> lineTo(args[0], args[1])
65 "l" -> relativeLineTo(args[0], args[1])
66 "Z" -> close()
67 else -> Log.v("Landroid", "unsupported SVG command: $cmd")
68 }
69 }
70 }
71