How to draw a Line with angle?

I want to use TLine to draw a line, but I can only get the coordinates of a point on the line and the Angle of the line. So I took a trick (as shown in the following code), but there is a problem with this, as the Angle approaches 90°, the resulting line length is extremely large. So I would like to ask you, is there a better way to draw a straight line, such as using Angle?

Thanks,
K.

double b_,xx1,xx2,yy1,yy2,k;
          k = delta[ievt]/57.3;
          k = tan(k);
//cout<<k<<endl;
          b_ = iy_b[ievt]-k*ix_b[ievt];
          xx1 = ix_b[ievt]-0.8;xx2 = ix_b[ievt]+0.8;
          yy1 = xx1*k+b_;
          yy2 = xx2*k+b_;
          TLine *line = new TLine(xx1,yy1,xx2,yy2);
          line->SetLineWidth(1);
          line->SetLineColor(kRed);
          line->Draw();

You need a bit of trigonometry:

{
   TCanvas *can = new TCanvas("can", "can", 600, 600);
   can->Range(-2.,-2.,2.,2.);
   double X, Y, phi, R;
   R = 1;
   phi = 0.0;
   while ( phi < 2*TMath::Pi() ){
      X = R*cos( phi );
      Y = R*sin( phi );
      TLine *L = new TLine( 0.,0.,X, Y);
      phi += TMath::Pi()/6.;
      L->Draw();
   }
}

Thanks!!
I was so stupid that I didn’t think of it at all !!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.