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 14, 2025
1 parent e0b4a0e commit e727edc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ru.tech.imageresizershrinker.core.ui.widget.modifier

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

Expand All @@ -39,10 +40,28 @@ data class Line(
endY = 1f
)

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

@Suppress("FunctionName")
fun Rotated(angle: Float): Line = CenterVertical.rotate(angle)
fun Rotated(angle: Float): Line = if (abs(angle) % 180 != 0f) {
CenterVertical.rotate(angle)
} else {
CenterVertical
}

@Suppress("FunctionName")
fun Bundle(size: Int): List<Line> = if (size > 0) {
List(size) {
Rotated(it * (360f / size))
}
} else {
listOf()
}
}

fun rotate(angle: Float): Line {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.smarttoolfactory.gesture.MotionEvent
import com.smarttoolfactory.gesture.pointerMotionEvents
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.launch
import net.engawapg.lib.zoomable.ZoomState
import net.engawapg.lib.zoomable.ZoomableDefaults.defaultZoomOnDoubleTap
Expand All @@ -83,6 +81,7 @@ import ru.tech.imageresizershrinker.feature.draw.domain.DrawLineStyle
import ru.tech.imageresizershrinker.feature.draw.domain.DrawMode
import ru.tech.imageresizershrinker.feature.draw.domain.DrawPathMode
import ru.tech.imageresizershrinker.feature.draw.presentation.components.utils.copy
import ru.tech.imageresizershrinker.feature.draw.presentation.components.utils.drawInfiniteLine
import ru.tech.imageresizershrinker.feature.draw.presentation.components.utils.drawRepeatedImageOnPath
import ru.tech.imageresizershrinker.feature.draw.presentation.components.utils.drawRepeatedTextOnPath
import ru.tech.imageresizershrinker.feature.draw.presentation.components.utils.mirrorIfNeeded
Expand Down Expand Up @@ -116,7 +115,7 @@ fun BitmapDrawer(
drawColor: Color,
drawLineStyle: DrawLineStyle = DrawLineStyle.None,
helperGridParams: HelperGridParams = remember { HelperGridParams() },
mirroringLines: ImmutableList<Line> = persistentListOf()
mirroringLines: List<Line> = remember { emptyList() }
) {
val scope = rememberCoroutineScope()

Expand Down Expand Up @@ -480,6 +479,10 @@ fun BitmapDrawer(
} else {
drawPath(androidPath, drawPaint)
}

mirroringLines.forEach {
drawInfiniteLine(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.NativeCanvas
import androidx.compose.ui.graphics.NativePaint
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
Expand All @@ -59,7 +58,6 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.applyCanvas
import coil3.request.ImageRequest
import coil3.toBitmap
import kotlinx.collections.immutable.ImmutableList
import ru.tech.imageresizershrinker.core.data.utils.safeConfig
import ru.tech.imageresizershrinker.core.domain.model.IntegerSize
import ru.tech.imageresizershrinker.core.domain.model.Pt
Expand All @@ -77,6 +75,8 @@ import ru.tech.imageresizershrinker.feature.draw.domain.DrawLineStyle
import ru.tech.imageresizershrinker.feature.draw.domain.DrawMode
import ru.tech.imageresizershrinker.feature.draw.domain.DrawPathMode
import kotlin.math.roundToInt
import kotlin.math.sqrt
import android.graphics.Paint as NativePaint
import android.graphics.Path as NativePath

internal fun Path.copy(): Path = NativePath(this.asAndroidPath()).asComposePath()
Expand Down Expand Up @@ -114,15 +114,15 @@ internal fun NativePath.mirror(

internal fun Path.mirrorIfNeeded(
canvasSize: IntegerSize,
mirroringLines: ImmutableList<Line>
mirroringLines: List<Line>
): Path = asAndroidPath().mirrorIfNeeded(
canvasSize = canvasSize,
mirroringLines = mirroringLines
).asComposePath()

internal fun NativePath.mirrorIfNeeded(
canvasSize: IntegerSize,
mirroringLines: ImmutableList<Line>
mirroringLines: List<Line>
): NativePath = if (mirroringLines.isNotEmpty()) {
NativePath(this).apply {
mirroringLines.forEach { mirroringLine ->
Expand Down Expand Up @@ -152,6 +152,48 @@ internal fun Path.mirror(
y1 = y1
).asComposePath()


fun Canvas.drawInfiniteLine(
line: Line,
paint: NativePaint = NativePaint().apply {
color = Color.Red.toArgb()
style = NativePaint.Style.STROKE
strokeWidth = 5f
}
) {
val width = width.toFloat()
val height = height.toFloat()

val startX = line.startX * width
val startY = line.startY * height
val endX = line.endX * width
val endY = line.endY * height

val dx = endX - startX
val dy = endY - startY

if (dx == 0f) {
drawLine(startX, 0f, startX, height, paint)
return
}

if (dy == 0f) {
drawLine(0f, startY, width, startY, paint)
return
}

val directionX = dx / sqrt(dx * dx + dy * dy)
val directionY = dy / sqrt(dx * dx + dy * dy)

val scale = maxOf(width, height) * 2
val extendedStartX = startX - directionX * scale
val extendedStartY = startY - directionY * scale
val extendedEndX = endX + directionX * scale
val extendedEndY = endY + directionY * scale

drawLine(extendedStartX, extendedStartY, extendedEndX, extendedEndY, paint)
}

internal fun ImageBitmap.clipBitmap(
path: Path,
paint: Paint,
Expand Down

0 comments on commit e727edc

Please sign in to comment.