Refactoring: move "norm" to Geometry.kt and add test

This commit is contained in:
Pierre-Yves Nicolas
2025-07-01 21:25:26 +02:00
parent 5e0cf451ea
commit 5a98f9f5e6
3 changed files with 39 additions and 7 deletions

View File

@@ -25,7 +25,6 @@ import org.opencv.core.Size
import org.opencv.imgproc.Imgproc
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.sqrt
fun detectDocumentQuad(mask: Bitmap, minQuadAreaRatio: Double = 0.02): Quad? {
val mat = Mat()
@@ -143,8 +142,3 @@ fun Point.toCv(): org.opencv.core.Point {
return org.opencv.core.Point(x.toDouble(), y.toDouble())
}
private fun norm(p1: Point, p2: Point): Double {
val dx = (p2.x - p1.x)
val dy = (p2.y - p1.y)
return sqrt(dx.toDouble() * dx + dy * dy)
}

View File

@@ -15,10 +15,21 @@
package org.mydomain.myscan
import kotlin.math.atan2
import kotlin.math.sqrt
data class Point(val x: Int, val y: Int)
data class Line(val from: Point, val to: Point)
data class Line(val from: Point, val to: Point) {
fun norm(): Double {
return norm(from, to)
}
}
fun norm(p1: Point, p2: Point): Double {
val dx = (p2.x - p1.x)
val dy = (p2.y - p1.y)
return sqrt(dx.toDouble() * dx + dy * dy)
}
data class Quad(
val topLeft: Point,

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 Pierre-Yves Nicolas
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.mydomain.myscan
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
class GeometryTest {
@Test
fun line() {
assertThat(Line(Point(0, 0), Point(10, 0)).norm()).isEqualTo(10.0)
assertThat(Line(Point(1, 2), Point(4, 6)).norm()).isEqualTo(5.0)
}
}