android图形开发工具,Android开发实现的几何图形工具类GeometryUtil完整实例

本文详细介绍了Android开发中的一个实用工具类GeometryUtil,该类提供了计算两点间距离、获取中点、按比例获取两点间点、以及求解直线与圆交点等几何操作的方法。对于Android图形编程具有参考价值。
摘要由CSDN通过智能技术生成

本文实例讲述了Android开发实现的几何图形工具类GeometryUtil。分享给大家供大家参考,具体如下:

package com.android.imooc.goo;

import android.graphics.PointF;

/**

* 几何图形工具

*/

public class GeometryUtil {

/**

* As meaning of method name. 获得两点之间的距离

*

* @param p0

* @param p1

* @return

*/

public static float getDistanceBetween2Points(PointF p0, PointF p1) {

float distance = (float) Math.sqrt(Math.pow(p0.y - p1.y, 2) + Math.pow(p0.x - p1.x, 2));

return distance;

}

/**

* Get middle point between p1 and p2. 获得两点连线的中点

*

* @param p1

* @param p2

* @return

*/

public static PointF getMiddlePoint(PointF p1, PointF p2) {

return new PointF((p1.x + p2.x) / 2.0f, (p1.y + p2.y) / 2.0f);

}

/**

* Get point between p1 and p2 by percent. 根据百分比获取两点之间的某个点坐标

*

* @param p1

* @param p2

* @param percent

* @return

*/

public static PointF getPointByPercent(PointF p1, PointF p2, float percent) {

return new PointF(evaluateValue(percent, p1.x, p2.x), evaluateValue(percent, p1.y, p2.y));

}

/**

* 根据分度值,计算从start到end中,fraction位置的值。fraction范围为0 -> 1

*

* @param fraction

* @param start

* @param end

* @return

*/

public static float evaluateValue(float fraction, Number start, Number end) {

return start.floatValue() + (end.floatValue() - start.floatValue()) * fraction;

}

/**

* Get the point of intersection between circle and line. 获取

* 通过指定圆心,斜率为lineK的直线与圆的交点。

*

* @param pMiddle

* The circle center point.

* @param radius

* The circle radius.

* @param lineK

* The slope of line which cross the pMiddle.

* @return

*/

public static PointF[] getIntersectionPoints(PointF pMiddle, float radius, Double lineK) {

PointF[] points = new PointF[2];

float radian, xOffset = 0, yOffset = 0;

if (lineK != null) {

radian = (float) Math.atan(lineK);

xOffset = (float) (Math.sin(radian) * radius);

yOffset = (float) (Math.cos(radian) * radius);

} else {

xOffset = radius;

yOffset = 0;

}

points[0] = new PointF(pMiddle.x + xOffset, pMiddle.y - yOffset);

points[1] = new PointF(pMiddle.x - xOffset, pMiddle.y + yOffset);

return points;

}

}

希望本文所述对大家Android程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值