需要掌握的內(nèi)容
-
難度應(yīng)該大多都在數(shù)學(xué)的三角函數(shù)中赁项,如果對這個知識點有問題的可以自行去學(xué)習(xí)一下
- sin角度=a/c
- cos角度=b/c
- tan角度=a/b
- cot角度=b/a
- sec角度=c/b
- csc角度=c/a
-
數(shù)學(xué)坐標(biāo)系和Android坐標(biāo)系的區(qū)別
y軸朝上是+吐根,x軸朝右是+
x軸朝右是+弯淘,y軸朝下是+
Demo代碼
class LinePathView : View{
//坐標(biāo)軸的paint
val paint: Paint=Paint()
val path: Path=Path()
//貝塞爾曲線的畫筆
val pPaint:Paint=Paint()
lateinit var center:PointF
constructor(context: Context) : super(context){
init()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: int) : super(
context,
attrs,
defStyleAttr
){
init()
}
private fun init() {
paint.isAntiAlias=true
paint.isDither=true
paint.color=Color.BLACK
paint.strokeWidth=3f
paint.style=Paint.Style.FILL
pPaint.isAntiAlias=true
pPaint.isDither=true
pPaint.color=Color.RED
pPaint.strokeWidth=3f
}
override fun onSizeChanged(w: int, h: int, oldw: int, oldh: int) {
super.onSizeChanged(w, h, oldw, oldh)
center=PointF((measuredWidth/2).tofloat(), (measuredHeight/2).tofloat())
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawLine((measuredWidth/2).tofloat(), 0F, (measuredWidth/2).tofloat(),
measuredHeight.tofloat(),paint)
canvas.drawLine(0f,(measuredHeight/2).tofloat(), measuredWidth.tofloat(),
(measuredHeight/2).tofloat(),paint)
//計算x軸上頂點的坐標(biāo)
val top = calPoint(center, (measuredHeight / 2).tofloat(), 90F)
val rightTop = calPoint(top, 80f, -45f)
canvas.drawLine(top.x,top.y,rightTop.x,rightTop.y,paint)
val leftTop = calPoint(top, 80f, -135f)
canvas.drawLine(top.x,top.y,leftTop.x,leftTop.y,paint)
//計算y軸右頂點的坐標(biāo)
val rightY = calPoint(center, (measuredWidth / 2).tofloat(), 360F)
val yTopLeft = calPoint(rightY, 80f, 135f)
canvas.drawLine(rightY.x,rightY.y,yTopLeft.x,yTopLeft.y,paint)
val yTopRight = calPoint(rightY, 80f, -135f)
canvas.drawLine(rightY.x,rightY.y,yTopRight.x,yTopRight.y,paint)
//計算貝塞爾曲線的一段的終點坐標(biāo)(起點是中心點)
val lastPoint = calPoint(center, 200f, 0f)
//計算貝塞爾曲線控制點的坐標(biāo)
val controlPoint = calPoint(center, 200f, 60f)
path.reset()
path.moveTo(center.x,center.y)
path.quadTo(controlPoint.x,controlPoint.y,lastPoint.x,lastPoint.y)
canvas.drawPath(path,pPaint)
//根據(jù)第一個貝塞爾曲線的終點畫第二個
val secondLastPoint = calPoint(lastPoint, 200f, 0f)
//第二個貝塞爾曲線的控制點
val secondControl = calPoint(lastPoint, 200f, -60f)
path.reset()
path.moveTo(lastPoint.x,lastPoint.y)
path.quadTo(secondControl.x,secondControl.y,secondLastPoint.x,secondLastPoint.y)
canvas.drawPath(path,pPaint)
//繪制梯形(以中心點為坐標(biāo)在第二象限找一個點作為梯形底邊的一個頂點)
val rightBottomPoint = calPoint(center, 200f, 135f)
val leftBottomPoint = calPoint(rightBottomPoint, 300f, -180f)
val leftTopPoint = calPoint(leftBottomPoint, 80f, 45f)
val rightTopPoint = calPoint(leftTopPoint, 200f, 0f)
path.reset()
path.moveTo(rightBottomPoint.x,rightBottomPoint.y)
path.lineTo(leftBottomPoint.x,leftBottomPoint.y)
path.lineTo(leftTopPoint.x,leftTopPoint.y)
path.lineTo(rightTopPoint.x,rightTopPoint.y)
canvas.drawPath(path,pPaint)
//在第三象限找一個點作為三角形的頂點
val sanTopPoint = calPoint(center, 300f, -135f)
val sanRightBottom = calPoint(sanTopPoint, 200f, -45f)
val sanLeftBottom = calPoint(sanRightBottom, 300f, 180f)
path.reset()
path.moveTo(sanTopPoint.x,sanTopPoint.y)
path.lineTo(sanLeftBottom.x,sanLeftBottom.y)
path.lineTo(sanRightBottom.x,sanRightBottom.y)
canvas.drawPath(path,pPaint)
}
fun calPoint(start: PointF,lineLength: float,angel: float): PointF {
//根據(jù)角度的cos值和斜邊的長度求出相鄰邊的長度,也就是x的偏移量
val x:float= (Math.cos(Math.toRadians(angel.todouble()))*lineLength).tofloat()
//根據(jù)角度的sin值和斜邊的長度求出相對邊的長度,也就是y的偏移量
val y:float= (Math.sin(Math.toRadians(angel.todouble()-180))*lineLength).tofloat()
return PointF(start.x+x,start.y+y)
}
}
運行結(jié)果
可能有點丑,但是只要內(nèi)部原理知道了狂丝,花點時間可以自己調(diào)一下角度和線的長度。