我用畫布來畫畫。
A、B、C 在同一條線上。有兩個點和 ax/y 軸值pointC:
Offset pointA = Offset(100, 200);
Offset pointB = Offset(200, 400);
double pointCx = 300;
Offset pointC = calculateOffsetByX(pointA, pointB, pointCx);
https://i.bmp.ovh/imgs/2021/12/0a7ee73b12749799.png
如果我有 x/y 值,我需要計算 c 的偏移量。
并封裝成這樣的函式:
/// @param [pointStart] start point of line
/// @param [point2] second point of line
/// @param [x] the position x of point need to calculate
/// @return Offset of the point on line
Offset calculateOffsetByX(Offset pointStart, Offset point2, double x){
// TODO
}
/// @param [pointStart] start point of line
/// @param [point2] second point of line
/// @param [y] the position y of point need to calculate
/// @return Offset of the point on line
Offset calculateOffsetByY(Offset pointStart, Offset point2, double y){
// TODO
}
有時偏移量不是正數,需要考慮這些條件:https : //i.bmp.ovh/imgs/2021/12/c6352f50f5dfb00d.png
uj5u.com熱心網友回復:
你可以試試這個功能。(我沒有測驗這段代碼,請嘗試這段代碼,如果它不起作用,請告訴我結果):
/// @param [pointStart] start point of line
/// @param [point2] second point of line
/// @param [x] the position x of point need to calculate
/// @return Offset of the point on line
Offset calculateOffsetByX(Offset pointStart, Offset point2, double x){
//y = ax b
final a = (pointStart.dy - point2.dy)/(pointStart.dx - point2.dx);
final b = pointStart.dy - a*pointStart.dx;
return Offset(x, a*x b);
}
/// @param [pointStart] start point of line
/// @param [point2] second point of line
/// @param [y] the position y of point need to calculate
/// @return Offset of the point on line
Offset calculateOffsetByY(Offset pointStart, Offset point2, double y){
//y = ax b
final a = (pointStart.dy - point2.dy)/(pointStart.dx - point2.dx);
final b = pointStart.dy - a*pointStart.dx;
return Offset((y - b)/a, y);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382592.html
下一篇:如何轉換為對數刻度并回傳?
