Skip to content

Commit

Permalink
Rotated Line added
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 13, 2025
1 parent e64bc24 commit e0b4a0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package ru.tech.imageresizershrinker.core.ui.widget.modifier

import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import kotlin.math.cos
import kotlin.math.sin

@Stable
@Immutable
Expand All @@ -37,12 +39,35 @@ data class Line(
endY = 1f
)

val CenterHorizontal = Line(
startX = 0f,
startY = 0.5f,
endX = 1f,
endY = 0.5f
)
val CenterHorizontal = Rotated(90f)

@Suppress("FunctionName")
fun Rotated(angle: Float): Line = CenterVertical.rotate(angle)
}

fun rotate(angle: Float): Line {
val centerX = (startX + endX) / 2
val centerY = (startY + endY) / 2

val radians = Math.toRadians(angle.toDouble()).toFloat()
val cosA = cos(radians)
val sinA = sin(radians)

fun rotatePoint(
x: Float,
y: Float
): Pair<Float, Float> {
val dx = x - centerX
val dy = y - centerY
val newX = centerX + dx * cosA - dy * sinA
val newY = centerY + dx * sinA + dy * cosA
return newX to newY
}

val (newStartX, newStartY) = rotatePoint(startX, startY)
val (newEndX, newEndY) = rotatePoint(endX, endY)

return copy(startX = newStartX, startY = newStartY, endX = newEndX, endY = newEndY)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,13 @@ fun BitmapDrawer(
}

if (drawMode is DrawMode.PathEffect && !isEraserOn) {
var shaderBitmap by remember {
var shaderBitmap by remember(
outputImage,
paths,
backgroundColor,
drawMode,
canvasSize
) {
mutableStateOf<ImageBitmap?>(null)
}

Expand All @@ -497,16 +503,18 @@ fun BitmapDrawer(
canvasSize
) {
scope.launch {
shaderBitmap = onRequestFiltering(
outputImage.asAndroidBitmap(),
transformationsForMode(
drawMode = drawMode,
canvasSize = canvasSize
)
)?.createScaledBitmap(
width = imageWidth,
height = imageHeight
)?.asImageBitmap()
if (shaderBitmap == null) {
shaderBitmap = onRequestFiltering(
outputImage.asAndroidBitmap(),
transformationsForMode(
drawMode = drawMode,
canvasSize = canvasSize
)
)?.createScaledBitmap(
width = imageWidth,
height = imageHeight
)?.asImageBitmap()
}
}
}

Expand Down

0 comments on commit e0b4a0e

Please sign in to comment.