莫名奇妙地用了叉积分。求完面积之后,再进行平均(除以y):
奇怪的是
pt2.fX = poly[0].fY - poly[3].fY;
pt2.fY = poly[3].fX - poly[0].fX;
这两行,竟然与逻辑推理出来的不一致。刚好取了个相反数。
static inline bool poly_to_point(SkPoint* pt, const SkPoint poly[], int count) {
float x = 1, y = 1;
SkPoint pt1, pt2;
if (count > 1) {
pt1.fX = poly[1].fX - poly[0].fX;
pt1.fY = poly[1].fY - poly[0].fY;
y = SkPoint::Length(pt1.fX, pt1.fY);
if (checkForZero(y)) {
return false;
}
switch (count) {
case 2:
break;
case 3:
pt2.fX = poly[0].fY - poly[2].fY;
pt2.fY = poly[2].fX - poly[0].fX;
goto CALC_X;
default:
pt2.fX = poly[0].fY - poly[3].fY;
pt2.fY = poly[3].fX - poly[0].fX;
CALC_X:
x = SkScalarDiv(SkScalarMul(pt1.fX, pt2.fX) +
SkScalarMul(pt1.fY, pt2.fY), y);
break;
}
}
pt->set(x, y);
return true;
}
2.从多边形到多边形。先转换为矩形,再转换为多边形。
bool SkMatrix::setPolyToPoly(const SkPoint src[], const SkPoint dst[],
int count) {
if ((unsigned)count > 4) {
SkDebugf("--- SkMatrix::setPolyToPoly count out of range %d\n", count);
return false;
}
if (0 == count) {
this->reset();
return true;
}
if (1 == count) {
this->setTranslate(dst[0].fX - src[0].fX, dst[0].fY - src[0].fY);
return true;
}
SkPoint scale;
if (!poly_to_point(&scale, src, count) ||
SkScalarNearlyZero(scale.fX) ||
SkScalarNearlyZero(scale.fY)) {
return false;
}
static const PolyMapProc gPolyMapProcs[] = {
SkMatrix::Poly2Proc, SkMatrix::Poly3Proc, SkMatrix::Poly4Proc
};
PolyMapProc proc = gPolyMapProcs[count - 2];
SkMatrix tempMap, result;
tempMap.setTypeMask(kUnknown_Mask);
if (!proc(src, &tempMap, scale)) {
return false;
}
if (!tempMap.invert(&result)) {
return false;
}
if (!proc(dst, &tempMap, scale)) {
return false;
}
if (!result.setConcat(tempMap, result)) {
return false;
}
*this = result;
return true;
}