Пример #1
0
function processGuitar4svg()
{
    global $guitar, $threshold;
    //test strings ends are on nut and bridge
    //if not dont to partials
    $numStrings = sizeof($guitar['strings']);
    $doPartials = true;
    $parallelFrets = true;
    $nut = new Segment($guitar['strings'][0]->end1(), $guitar['strings'][$numStrings - 1]->end1());
    $bridge = new Segment($guitar['strings'][0]->end2(), $guitar['strings'][$numStrings - 1]->end2());
    $midline = new Segment(new Point(($nut->end2->x + $nut->end1->x) / 2, ($nut->end2->y + $nut->end1->y) / 2), new Point(($bridge->end2->x + $bridge->end1->x) / 2, ($bridge->end2->y + $bridge->end1->y) / 2));
    foreach ($guitar['strings'] as $string) {
        if (!($nut->distanceToPoint($string->end1()) < $threshold) || !($bridge->distanceToPoint($string->end2()) < $threshold)) {
            echo $string->toString(), '<br />';
            echo $nut->distanceToPoint($string->end1()), '<br />';
            echo $bridge->distanceToPoint($string->end2()), '<br />';
            $doPartials = false;
            break;
        }
    }
    $denom = ($bridge->end2->y - $bridge->end1->y) * ($nut->end2->x - $nut->end1->x) - ($bridge->end2->x - $bridge->end1->x) * ($nut->end2->y - $nut->end1->y);
    if ($denom != 0) {
        $parallelFrets = false;
    }
    $intersection = intersect($nut, $bridge);
    $strings = array();
    $tones = sizeof($guitar['scale']['steps']) - 1;
    $totalWidth = array();
    $scale = $guitar['scale']['steps'];
    for ($i = 0; $i < $numStrings; $i++) {
        $base = $guitar['tuning'][$i];
        $frets = array();
        $frets[0]['fret'] = $doPartials ? new Segment($guitar['meta'][$i]->end1, $guitar['meta'][$i + 1]->end1) : new Segment($guitar['strings'][$i]->end1, $guitar['strings'][$i]->end1);
        $frets[0]['intersection'] = $guitar['strings'][$i]->end1;
        for ($j = 1; $j <= $guitar['frets']; $j++) {
            $step = ($base + $j - 1) % $tones + 1;
            //		$step=(($base+$j)%($tones-1))+1;
            //		$step=$step==0?1:$step;
            $ratio = 1 - $scale[$step][1] * $scale[$step - 1][0] / ($scale[$step][0] * $scale[$step - 1][1]);
            $x = $frets[$j - 1]['intersection']->x + $ratio * ($guitar['strings'][$i]->end2->x - $frets[$j - 1]['intersection']->x);
            $y = $frets[$j - 1]['intersection']->y + $ratio * ($guitar['strings'][$i]->end2->y - $frets[$j - 1]['intersection']->y);
            $frets[$j]['intersection'] = new Point($x, $y);
            $temp = new Segment($guitar['strings'][$i]->end1(), $frets[$j]['intersection']);
            $frets[$j]['nutDist'] = $temp->length();
            $frets[$j]['totalRatio'] = $frets[$j]['nutDist'] / $guitar['strings'][$i]->length();
            if ($doPartials) {
                /*			//partials depending on nut bridge intersection (bad)
                				$temp=$parallelFrets?
                					$nut->createParallel($frets[$j]['intersection']):
                					new Segment($intersection,$frets[$j]['intersection']);
                				$frets[$j]['fret']=new Segment(intersect($temp,$guitar['meta'][$i]),
                						intersect($temp,$guitar['meta'][$i+1]));
                				//partials depending on meta lines (questionable)
                				if ($parallelFrets)
                				{
                					$frets[$j]['fret']=$nut->createParallel($frets[$j]['intersection']);
                				}
                				else
                				{
                					$frets[$j]['fret']=new Segment(
                						$guitar['meta'][$i]->pointAt($guitar['meta'][$i]->length()*
                							$frets[$j]['totalRatio']),
                						$guitar['meta'][$i+1]->pointAt($guitar['meta'][$i+1]->length()*
                							$frets[$j]['totalRatio'])
                						);
                				}
                	*/
                //partials depending on outer strings (questionable)
                if ($parallelFrets) {
                    $temp = $nut->createParallel($frets[$j]['intersection']);
                } else {
                    $temp = new Segment($guitar['strings'][0]->pointAt($guitar['strings'][0]->length() * $frets[$j]['totalRatio']), $guitar['strings'][$numStrings - 1]->pointAt($guitar['strings'][$numStrings - 1]->length() * $frets[$j]['totalRatio']));
                }
                $frets[$j]['fret'] = new Segment(intersect($temp, $guitar['meta'][$i]), intersect($temp, $guitar['meta'][$i + 1]));
            } else {
                $frets[$j]['fret'] = new Segment($frets[$j]['intersection'], $frets[$j]['intersection']);
            }
        }
        $strings[] = $frets;
    }
    return array('strings' => $strings, 'midline' => $midline, 'nut' => $nut, 'bridge' => $bridge);
}