Skip to content

Commit

Permalink
CircleShape -> EllipseShape
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmenstar1 committed Jul 10, 2023
1 parent 1cae650 commit 120ab5d
Showing 1 changed file with 6 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.*
import androidx.core.graphics.component1
import androidx.core.graphics.component2
import com.github.pelmenstar1.rangecalendar.utils.SQRT_2
import kotlin.math.max
import kotlin.math.sqrt

/**
Expand All @@ -24,13 +25,6 @@ interface Shape {
@JvmName("needsPathToDraw")
get

/**
* Throws an exception if [box] is invalid.
*
* @param box rectangle in which shape in located.
*/
fun validateBox(box: RectF)

/**
* Adds a shape to specified [path].
*
Expand Down Expand Up @@ -68,9 +62,6 @@ object RectangleShape : Shape {
override val needsPathToDraw: Boolean
get() = false

override fun validateBox(box: RectF) {
}

override fun addToPath(path: Path, box: RectF) {
path.addRect(box, Path.Direction.CW)
}
Expand Down Expand Up @@ -102,19 +93,12 @@ object RectangleShape : Shape {
}

/**
* A circle shape.
* Note, this is not ellipse, hence width and height of a box, in which a circle is drawn into, should be equal.
* Ellipse.
*/
object CircleShape : Shape {
object EllipseShape : Shape {
override val needsPathToDraw: Boolean
get() = false

override fun validateBox(box: RectF) {
require(box.width() == box.height()) {
"Width and shape of a box should be equal"
}
}

override fun draw(canvas: Canvas, box: RectF, paint: Paint) {
canvas.drawOval(box, paint)
}
Expand All @@ -127,13 +111,13 @@ object CircleShape : Shape {
outCenter.x = box.centerX()
outCenter.y = box.centerY()

return box.width() * 0.5f
return max(box.width(), box.height()) * 0.5f
}
}

/**
* A triangle with points defined by relative points(x and y are within range `[0; 1]`).
* Such definition allows a triangle to be scalable.
* A triangle with points defined by relative points (x and y are within range `[0; 1]`).
* Such definition allows the triangle to be scalable.
*
* @param p1x x-axis of 1 relative point
* @param p1y y-axis of 1 relative point
Expand All @@ -158,9 +142,6 @@ class TriangleShape(
requireValidRelativePoint(p3x, p3y)
}

override fun validateBox(box: RectF) {
}

override fun draw(canvas: Canvas, box: RectF, paint: Paint) {
throw IllegalStateException("TriangleShape requires a Path to draw")
}
Expand Down

0 comments on commit 120ab5d

Please sign in to comment.