Search code examples
phpwordpressgraphshapestcpdf

Calculate start and end of a line to create a pentagon using Line() function TCPDF


I am aware that TCPDF have inbuilt functions to create different shapes. I have a case where I have to create a line and place dots as points (8,2) on it considering 0 as center of the line. Using tcpdf line() function. I created a line and placed dots on it see the screenshot below. Image with dots

Here in this screenshot points (8,2) are placed on a line where 8 is the left point from the center of the line and 2 is the right point from center of the line. Now I have to rotate the line to 72 degrees and place the next line with (left,right) points. Repeat the same process for total 5 lines with dynamic (left,right) points.

Here is what I am currently using:

    $counter = 1;
    foreach($graph_points as $points){
        // center of the line
        $xc = 100;
        $yc = 150;

        if($counter > 1){
            $pdf->Rotate(-72, $xc, $yc);
        }
        
        $left = round($points->left);
        $left_perc = (($left*10)/100) * 50;


        $right = round($points->right);
        $right_perc = (($right*10)/100) * 50;


        $pdf->SetDrawColor(200, 200, 200);

        $pdf->Line($xc - 50 , $yc, $xc + 50 , $yc,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
        
        $pdf->Line($xc, $yc - 2, $xc, $yc + 2,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));

        $pdf->Circle($xc - 50 + (50 - $left_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));

        $pdf->Text($xc - 53 + (50 - $left_perc), $yc - 7, $left, false, false, true, 0, 0, '', 0, '', 0, false, 'T', 'T');

        $pdf->Circle($xc + 50 - (50 - $right_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));

        $pdf->Text($xc + 47 - (50 - $right_perc), $yc - 7, $right, false, false, true, 0, 0, '', 0, '', 0, false, 'T', 'T');

        $counter++;
    }

Below screenshot is the output of this code and I want a pentagonal shape with 5 lines and their points on it. enter image description here

I have been trying this from 2 days and didn't found any working solution. Please suggest me to make calculations in a way to create pentagon using lines.

Thanks

Update

I managed to create the shape but side are not evenly connected to each other also it affected the text placement of numbers.

Here is the code:

    $counter = 1;
    $xc = 100;
    $yc = 150;
    foreach($graph_points as $points){
        // center of the line
       
        if($counter > 1){
            $xc = $xc - 65;
            $yc = $yc - 45;
            $pdf->Rotate(-72, $xc, $yc);
        }
        
        $left = round($points->left);
        $left_perc = (($left*10)/100) * 50;


        $right = round($points->right);
        $right_perc = (($right*10)/100) * 50;


        $pdf->SetDrawColor(200, 200, 200);

        $pdf->Line($xc - 50 , $yc, $xc + 50 , $yc,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
        
        $pdf->Line($xc, $yc - 2, $xc, $yc + 2,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));

        $pdf->Circle($xc - 50 + (50 - $left_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));

        $pdf->Text($xc - 53 + (50 - $left_perc), $yc, $left, false, false, true, 0, 0, '', 0, '', 0, false, 'T', 'T');

        $pdf->Circle($xc + 50 - (50 - $right_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));

        $pdf->Text($xc + 47 - (50 - $right_perc), $yc, $right, false, false, true, 0, 0, '', 0, '', 0, false, 'T', 'T');

        $counter++;
    }

Here is the screenshot how it looks now. enter image description here


Solution

  • After trying so hard for 3 days, I finally found a working solution. Posting my solution here in case anyone have same requirement.

        $xc = 100;
        $yc = 200;
        $counter = 1;
        foreach($graph_points as $points){
            if($counter > 1){
                $xc = $xc - 39;
                $yc = $yc - 28;
                $pdf->Rotate(-72, $xc, $yc);
            }
            
            $left = round($points->left);
            $left_perc = (($left*10)/100) * 30;
    
    
            $right = round($points->right);
            $right_perc = (($right*10)/100) * 30;
    
            $pdf->SetDrawColor(200, 200, 200);
    
            $pdf->Line($xc - 30 , $yc, $xc + 30 , $yc,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
            
            $pdf->Line($xc, $yc - 2, $xc, $yc + 2,array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
    
            $pdf->Circle($xc - 30 + (30 - $left_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));
    
            $pdf->Circle($xc + 30 - (30 - $right_perc), $yc, 1, 0, 360, 'DF', array('width' => 0.25, 'dash' => 0, 'color' => array(255,0,0)), array(210, 0, 0));
    
            $counter++;
        }