php - centre text between 2 coords -
i trying centre line of text, of known width along line specified start , end coordinates.
the purpose write text around polygon, lines not horizontal.
currently have following function takes start x , y , finish x , y of line , width of text (in pixels).
the text drawn starting @ x1, y1 @ correct angle follow line.
to centre text on line have tried calculate left padding in pixels should applied x1, y1 move text correct amount left origin.
the following function attempt @ modifying coordinates implement above concept. not quite right. end text off line, x out y, depends on face neither x or y correct.
private function centertextonline(&$x1, &$y1, &$x2, &$y2, $width) { $distance = $this->getdistance($x1, $y1, $x2, $y2); //calculate left padding required in pixels $padding = ($distance - $width) / 2; //what factor need alter x1, y1 by? $factor = ($distance / $padding); $gradient = ($y2-$y1)/($x2-$x1); //gradient alter y $x1 += abs($x2-$x1) / $factor; //move start x factor $y1 += ($gradient / $factor); //add factor of gradient start y return; }
if can see error, or knows algorithm purpose, appreciate input.
thanks time.
i think should work:
$y1 += ($y2-$y1)/$factor; $k = $how_much_distance_i_want/sqrt(1+$gradient*$gradient); $x1 += $k*$gradient; $y1 -= $k; return;
while @ it, think abs
in
$x1 += abs($x2-$x1) / $factor;
can lead unexpected results in case ($x2-$x1)
negative (maybe better leave without abs
), , depending on how php deals infinities, might want have plan case $factor === 0
.
Comments
Post a Comment