35 TLine::TLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
36 :TObject(), TAttLine()
38 fX1=x1; fY1=y1; fX2=x2; fY2=y2;
45 TLine::TLine(
const TLine &line) : TObject(line), TAttLine(line), TAttBBox2D(line)
47 line.TLine::Copy(*
this);
53 TLine &TLine::operator=(
const TLine &src)
55 src.TLine::Copy(*
this);
62 void TLine::Copy(TObject &obj)
const
65 TAttLine::Copy(((TLine&)obj));
66 ((TLine&)obj).fX1 = fX1;
67 ((TLine&)obj).fY1 = fY1;
68 ((TLine&)obj).fX2 = fX2;
69 ((TLine&)obj).fY2 = fY2;
75 Int_t TLine::DistancetoPrimitive(Int_t px, Int_t py)
77 if (!TestBit(kLineNDC))
return DistancetoLine(px,py,gPad->XtoPad(fX1),gPad->YtoPad(fY1),gPad->XtoPad(fX2),gPad->YtoPad(fY2));
78 Double_t x1 = gPad->GetX1() + fX1*(gPad->GetX2()-gPad->GetX1());
79 Double_t y1 = gPad->GetY1() + fY1*(gPad->GetY2()-gPad->GetY1());
80 Double_t x2 = gPad->GetX1() + fX2*(gPad->GetX2()-gPad->GetX1());
81 Double_t y2 = gPad->GetY1() + fY2*(gPad->GetY2()-gPad->GetY1());
82 return DistancetoLine(px,py,x1,y1,x2,y2);
88 TLine *TLine::DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
90 TLine *newline =
new TLine(x1, y1, x2, y2);
91 TAttLine::Copy(*newline);
92 newline->SetBit(kCanDelete);
100 TLine *TLine::DrawLineNDC(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
102 TLine *newline = DrawLine(x1, y1, x2, y2);
103 newline->SetBit(kLineNDC);
117 void TLine::ExecuteEvent(Int_t event, Int_t px, Int_t py)
122 static Int_t d1,d2,px1,px2,py1,py2;
123 static Int_t pxold, pyold, px1old, py1old, px2old, py2old;
124 static Double_t oldX1, oldY1, oldX2, oldY2;
125 static Bool_t p1, p2, pL, ndcsav;
126 Double_t dpx,dpy,xp1,yp1;
129 Bool_t opaque = gPad->OpaqueMoving();
131 if (!gPad->IsEditable())
return;
141 ndcsav = TestBit(kLineNDC);
143 gVirtualX->SetLineColor(-1);
151 if (TestBit(kLineNDC)) {
152 px1 = gPad->UtoPixel(GetX1());
153 py1 = gPad->VtoPixel(GetY1());
154 px2 = gPad->UtoPixel(GetX2());
155 py2 = gPad->VtoPixel(GetY2());
157 px1 = gPad->XtoAbsPixel(gPad->XtoPad(GetX1()));
158 py1 = gPad->YtoAbsPixel(gPad->YtoPad(GetY1()));
159 px2 = gPad->XtoAbsPixel(gPad->XtoPad(GetX2()));
160 py2 = gPad->YtoAbsPixel(gPad->YtoPad(GetY2()));
162 p1 = p2 = pL = kFALSE;
164 d1 = abs(px1 - px) + abs(py1-py);
166 px1old = px1; py1old = py1;
168 gPad->SetCursor(kPointer);
171 d2 = abs(px2 - px) + abs(py2-py);
173 px2old = px2; py2old = py2;
175 gPad->SetCursor(kPointer);
180 pxold = px; pyold = py;
181 gPad->SetCursor(kMove);
185 case kArrowKeyRelease:
190 gVirtualX->DrawLine(px1old, py1old, px2, py2);
191 gVirtualX->DrawLine(px, py, px2, py2);
195 SetX2(gPad->GetX1() + oldX2*(gPad->GetX2()-gPad->GetX1()));
196 SetY2(gPad->GetY1() + oldY2*(gPad->GetY2()-gPad->GetY1()));
198 SetX1(gPad->AbsPixeltoX(px));
199 SetY1(gPad->AbsPixeltoY(py));
206 gVirtualX->DrawLine(px1, py1, px2old, py2old);
207 gVirtualX->DrawLine(px1, py1, px, py);
211 SetX1(gPad->GetX1() + oldX1*(gPad->GetX2()-gPad->GetX1()));
212 SetY1(gPad->GetY1() + oldY1*(gPad->GetY2()-gPad->GetY1()));
214 SetX2(gPad->AbsPixeltoX(px));
215 SetY2(gPad->AbsPixeltoY(py));
221 if (!opaque) gVirtualX->DrawLine(px1, py1, px2, py2);
222 dx = px-pxold; dy = py-pyold;
223 px1 += dx; py1 += dy; px2 += dx; py2 += dy;
224 if (!opaque) gVirtualX->DrawLine(px1, py1, px2, py2);
228 if (ndcsav) SetNDC(kFALSE);
229 SetX1(gPad->AbsPixeltoX(px1));
230 SetY1(gPad->AbsPixeltoY(py1));
231 SetX2(gPad->AbsPixeltoX(px2));
232 SetY2(gPad->AbsPixeltoY(py2));
238 if (GetX1() > GetX2()) {
239 if (GetY1() > GetY2())
240 gPad->ShowGuidelines(
this, event,
'2',
true);
242 gPad->ShowGuidelines(
this, event,
'3',
true);
244 if (GetY1() > GetY2())
245 gPad->ShowGuidelines(
this, event,
'1',
true);
247 gPad->ShowGuidelines(
this, event,
'4',
true);
252 if (GetX1() > GetX2()) {
253 if (GetY1() > GetY2())
254 gPad->ShowGuidelines(
this, event,
'4',
true);
256 gPad->ShowGuidelines(
this, event,
'1',
true);
258 if (GetY1() > GetY2())
259 gPad->ShowGuidelines(
this, event,
'3',
true);
261 gPad->ShowGuidelines(
this, event,
'2',
true);
265 gPad->ShowGuidelines(
this, event,
'i',
true);
267 gPad->Modified(kTRUE);
274 if (gROOT->IsEscaped()) {
275 gROOT->SetEscape(kFALSE);
281 gPad->Modified(kTRUE);
287 if (ndcsav && !TestBit(kLineNDC)) {
288 SetX1((GetX1() - gPad->GetX1())/(gPad->GetX2()-gPad->GetX1()));
289 SetX2((GetX2() - gPad->GetX1())/(gPad->GetX2()-gPad->GetX1()));
290 SetY1((GetY1() - gPad->GetY1())/(gPad->GetY2()-gPad->GetY1()));
291 SetY2((GetY2() - gPad->GetY1())/(gPad->GetY2()-gPad->GetY1()));
294 gPad->ShowGuidelines(
this, event);
296 if (TestBit(kLineNDC)) {
297 dpx = gPad->GetX2() - gPad->GetX1();
298 dpy = gPad->GetY2() - gPad->GetY1();
302 SetX1((gPad->AbsPixeltoX(px)-xp1)/dpx);
303 SetY1((gPad->AbsPixeltoY(py)-yp1)/dpy);
306 SetX2((gPad->AbsPixeltoX(px)-xp1)/dpx);
307 SetY2((gPad->AbsPixeltoY(py)-yp1)/dpy);
310 SetX1((gPad->AbsPixeltoX(px1)-xp1)/dpx);
311 SetY1((gPad->AbsPixeltoY(py1)-yp1)/dpy);
312 SetX2((gPad->AbsPixeltoX(px2)-xp1)/dpx);
313 SetY2((gPad->AbsPixeltoY(py2)-yp1)/dpy);
317 SetX1(gPad->PadtoX(gPad->AbsPixeltoX(px)));
318 SetY1(gPad->PadtoY(gPad->AbsPixeltoY(py)));
321 SetX2(gPad->PadtoX(gPad->AbsPixeltoX(px)));
322 SetY2(gPad->PadtoY(gPad->AbsPixeltoY(py)));
325 SetX1(gPad->PadtoX(gPad->AbsPixeltoX(px1)));
326 SetY1(gPad->PadtoY(gPad->AbsPixeltoY(py1)));
327 SetX2(gPad->PadtoX(gPad->AbsPixeltoX(px2)));
328 SetY2(gPad->PadtoY(gPad->AbsPixeltoY(py2)));
331 if (TestBit(kVertical)) {
332 if (p1) SetX2(GetX1());
333 if (p2) SetX1(GetX2());
335 if (TestBit(kHorizontal)) {
336 if (p1) SetY2(GetY1());
337 if (p2) SetY1(GetY2());
339 gPad->Modified(kTRUE);
341 if (!opaque) gVirtualX->SetLineColor(-1);
347 ExecuteEvent(kButton1Down, px, py);
350 event = gVirtualX->RequestLocator(1,1,px,py);
352 ExecuteEvent(kButton1Motion, px, py);
355 ExecuteEvent(kButton1Up, px, py);
365 void TLine::ls(Option_t *)
const
367 TROOT::IndentLevel();
368 printf(
"%s X1=%f Y1=%f X2=%f Y2=%f\n",IsA()->GetName(),fX1,fY1,fX2,fY2);
374 void TLine::Paint(Option_t *)
376 if (TestBit(kLineNDC)) PaintLineNDC(fX1,fY1,fX2,fY2);
377 else PaintLine(gPad->XtoPad(fX1),gPad->YtoPad(fY1),gPad->XtoPad(fX2),gPad->YtoPad(fY2));
383 void TLine::PaintLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
386 gPad->PaintLine(x1,y1,x2,y2);
392 void TLine::PaintLineNDC(Double_t u1, Double_t v1, Double_t u2, Double_t v2)
395 gPad->PaintLineNDC(u1,v1,u2,v2);
401 void TLine::Print(Option_t *)
const
403 printf(
"%s X1=%f Y1=%f X2=%f Y2=%f",IsA()->GetName(),fX1,fY1,fX2,fY2);
404 if (GetLineColor() != 1) printf(
" Color=%d",GetLineColor());
405 if (GetLineStyle() != 1) printf(
" Style=%d",GetLineStyle());
406 if (GetLineWidth() != 1) printf(
" Width=%d",GetLineWidth());
413 void TLine::SavePrimitive(std::ostream &out, Option_t * )
415 if (gROOT->ClassSaved(TLine::Class())) {
420 out<<
"line = new TLine("<<fX1<<
","<<fY1<<
","<<fX2<<
","<<fY2<<
");"<<std::endl;
422 SaveLineAttributes(out,
"line",1,1,1);
424 if (TestBit(kLineNDC))
425 out<<
" line->SetNDC();"<<std::endl;
427 out<<
" line->Draw();"<<std::endl;
433 Bool_t TLine::IsHorizontal()
435 return TestBit(kHorizontal);
441 Bool_t TLine::IsVertical()
443 return TestBit(kVertical);
449 void TLine::SetNDC(Bool_t isNDC)
452 if (isNDC) SetBit(kLineNDC);
460 void TLine::SetHorizontal(Bool_t set )
462 SetBit(kHorizontal, set);
465 Int_t px1 = gPad->XtoAbsPixel(fX1);
466 Int_t px2 = gPad->XtoAbsPixel(fX2);
467 Int_t py1 = gPad->YtoAbsPixel(fY1);
468 Int_t py2 = gPad->YtoAbsPixel(fY2);
469 Int_t l = Int_t(TMath::Sqrt((px2-px1)*(px2-px1)+(py2-py1)*(py2-py1)));
470 if (fX2 >= fX1) fX2 = gPad->AbsPixeltoX(px1+l);
471 else fX2 = gPad->AbsPixeltoX(px1-l);
481 void TLine::SetVertical(Bool_t set )
483 SetBit(kVertical, set);
485 SetHorizontal(kFALSE);
486 Int_t px1 = gPad->XtoAbsPixel(fX1);
487 Int_t px2 = gPad->XtoAbsPixel(fX2);
488 Int_t py1 = gPad->YtoAbsPixel(fY1);
489 Int_t py2 = gPad->YtoAbsPixel(fY2);
490 Int_t l = Int_t(TMath::Sqrt((px2-px1)*(px2-px1)+(py2-py1)*(py2-py1)));
491 if (fY2 >= fY1) fY2 = gPad->AbsPixeltoY(py1-l);
492 else fY2 = gPad->AbsPixeltoY(py1+l);
500 void TLine::Streamer(TBuffer &R__b)
502 if (R__b.IsReading()) {
504 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
506 R__b.ReadClassBuffer(TLine::Class(),
this, R__v, R__s, R__c);
510 TObject::Streamer(R__b);
511 TAttLine::Streamer(R__b);
513 R__b >> x1; fX1 = x1;
514 R__b >> y1; fY1 = y1;
515 R__b >> x2; fX2 = x2;
516 R__b >> y2; fY2 = y2;
520 R__b.WriteClassBuffer(TLine::Class(),
this);
526 Rectangle_t TLine::GetBBox()
529 Int_t px1, py1, px2, py2;
530 px1 = gPad->XtoPixel(fX1);
531 px2 = gPad->XtoPixel(fX2);
532 py1 = gPad->YtoPixel(fY1);
533 py2 = gPad->YtoPixel(fY2);
536 if (px1>px2) { tmp = px1; px1 = px2; px2 = tmp;}
537 if (py1>py2) { tmp = py1; py1 = py2; py2 = tmp;}
541 BBox.fWidth = px2-px1;
542 BBox.fHeight = py2-py1;
550 TPoint TLine::GetBBoxCenter()
553 p.SetX(gPad->XtoPixel(TMath::Min(fX1,fX2)+0.5*(TMath::Max(fX1, fX2)-TMath::Min(fX1, fX2))));
554 p.SetY(gPad->YtoPixel(TMath::Min(fY1,fY2)+0.5*(TMath::Max(fY1, fY2)-TMath::Min(fY1, fY2))));
561 void TLine::SetBBoxCenter(
const TPoint &p)
563 Double_t w = TMath::Max(fX1, fX2)-TMath::Min(fX1, fX2);
564 Double_t h = TMath::Max(fY1, fY2)-TMath::Min(fY1, fY2);
566 this->SetX1(gPad->PixeltoX(p.GetX())-0.5*w);
567 this->SetX2(gPad->PixeltoX(p.GetX())+0.5*w);
570 this->SetX2(gPad->PixeltoX(p.GetX())-0.5*w);
571 this->SetX1(gPad->PixeltoX(p.GetX())+0.5*w);
574 this->SetY1(gPad->PixeltoY(p.GetY()-gPad->VtoPixel(0))-0.5*h);
575 this->SetY2(gPad->PixeltoY(p.GetY()-gPad->VtoPixel(0))+0.5*h);
578 this->SetY2(gPad->PixeltoY(p.GetY()-gPad->VtoPixel(0))-0.5*h);
579 this->SetY1(gPad->PixeltoY(p.GetY()-gPad->VtoPixel(0))+0.5*h);
586 void TLine::SetBBoxCenterX(
const Int_t x)
588 Double_t w = TMath::Max(fX1, fX2)-TMath::Min(fX1, fX2);
590 this->SetX1(gPad->PixeltoX(x)-0.5*w);
591 this->SetX2(gPad->PixeltoX(x)+0.5*w);
594 this->SetX2(gPad->PixeltoX(x)-0.5*w);
595 this->SetX1(gPad->PixeltoX(x)+0.5*w);
602 void TLine::SetBBoxCenterY(
const Int_t y)
604 Double_t h = TMath::Max(fY1, fY2)-TMath::Min(fY1, fY2);
606 this->SetY1(gPad->PixeltoY(y-gPad->VtoPixel(0))-0.5*h);
607 this->SetY2(gPad->PixeltoY(y-gPad->VtoPixel(0))+0.5*h);
610 this->SetY2(gPad->PixeltoY(y-gPad->VtoPixel(0))-0.5*h);
611 this->SetY1(gPad->PixeltoY(y-gPad->VtoPixel(0))+0.5*h);
619 void TLine::SetBBoxX1(
const Int_t x)
622 this->SetX1(gPad->PixeltoX(x));
624 this->SetX2(gPad->PixeltoX(x));
631 void TLine::SetBBoxX2(
const Int_t x)
634 this->SetX2(gPad->PixeltoX(x));
636 this->SetX1(gPad->PixeltoX(x));
642 void TLine::SetBBoxY1(
const Int_t y)
645 this->SetY2(gPad->PixeltoY(y - gPad->VtoPixel(0)));
647 this->SetY1(gPad->PixeltoY(y - gPad->VtoPixel(0)));
654 void TLine::SetBBoxY2(
const Int_t y)
657 this->SetY1(gPad->PixeltoY(y - gPad->VtoPixel(0)));
659 this->SetY2(gPad->PixeltoY(y - gPad->VtoPixel(0)));