Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMarker.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Rene Brun 12/05/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <cstdlib>
13#include <iostream>
14
15#include "TROOT.h"
16#include "TBuffer.h"
17#include "TVirtualPad.h"
18#include "TMarker.h"
19#include "TVirtualX.h"
20#include "TMath.h"
21#include "TPoint.h"
22#include "TText.h"
23#include "snprintf.h"
24
26
27
28/** \class TMarker
29\ingroup BasicGraphics
30
31Manages Markers. Marker attributes are managed by TAttMarker.
32*/
33
34////////////////////////////////////////////////////////////////////////////////
35/// Marker default constructor.
36
38{
39 fX = 0;
40 fY = 0;
41}
42
43////////////////////////////////////////////////////////////////////////////////
44/// Marker normal constructor.
45
48{
49 fX = x;
50 fY = y;
51 fMarkerStyle = marker;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Marker default destructor.
56
58{
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Marker copy constructor.
63
64TMarker::TMarker(const TMarker &marker) : TObject(marker), TAttMarker(marker), TAttBBox2D(marker)
65{
66 fX = 0;
67 fY = 0;
68 ((TMarker&)marker).Copy(*this);
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Copy this marker to marker.
73
74void TMarker::Copy(TObject &obj) const
75{
76 TObject::Copy(obj);
78 ((TMarker&)obj).fX = fX;
79 ((TMarker&)obj).fY = fY;
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Display the table of markers with their numbers.
84
86{
87 TMarker *marker = new TMarker();
88 marker->SetMarkerSize(3);
89 TText *text = new TText();
90 text->SetTextFont(62);
91 text->SetTextAlign(22);
92 text->SetTextSize(0.1);
93 char atext[] = " ";
94 Double_t x = 0;
95 Double_t dx = 1/16.0;
96 for (Int_t i=1;i<16;i++) {
97 x += dx;
98 snprintf(atext,7,"%d",i);
99 marker->SetMarkerStyle(i);
100 marker->DrawMarker(x,.25);
101 text->DrawText(x,.12,atext);
102 snprintf(atext,7,"%d",i+19);
103 marker->SetMarkerStyle(i+19);
104 marker->DrawMarker(x,.55);
105 text->DrawText(x,.42,atext);
106 snprintf(atext,7,"%d",i+34);
107 marker->SetMarkerStyle(i+34);
108 marker->DrawMarker(x,.85);
109 text->DrawText(x,.72,atext);
110 }
111 delete marker;
112 delete text;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Display the table of markers with different line widths and their numbers.
117
119{
120 TMarker *marker = new TMarker();
121 marker->SetMarkerSize(3);
122 TText *text = new TText();
123 text->SetTextFont(62);
124 text->SetTextAlign(22);
125 text->SetTextSize(0.075);
126 char atext[] = " ";
127 Double_t x = 0;
128 Double_t dx = 1/19.0;
129 for (Int_t i=1;i<19;i++) {
130 x += dx;
131 snprintf(atext,7,"%d",i+49);
132 marker->SetMarkerStyle(i+49);
133 marker->DrawMarker(x,0.19);
134 text->DrawText(x,0.08,atext);
135 snprintf(atext,7,"%d",i+67);
136 marker->SetMarkerStyle(i+67);
137 marker->DrawMarker(x,0.42);
138 text->DrawText(x,0.31,atext);
139 snprintf(atext,7,"%d",i+85);
140 marker->SetMarkerStyle(i+85);
141 marker->DrawMarker(x,0.65);
142 text->DrawText(x,0.54,atext);
143 snprintf(atext,7,"%d",i+103);
144 marker->SetMarkerStyle(i+103);
145 marker->DrawMarker(x,0.88);
146 text->DrawText(x,0.77,atext);
147 }
148 delete marker;
149 delete text;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Compute distance from point px,py to a marker.
154///
155/// Compute the closest distance of approach from point px,py to this marker.
156/// The distance is computed in pixels units.
157
159{
160 Int_t pxm, pym;
161 if (TestBit(kMarkerNDC)) {
162 pxm = gPad->UtoPixel(fX);
163 pym = gPad->VtoPixel(fY);
164 } else {
165 pxm = gPad->XtoAbsPixel(gPad->XtoPad(fX));
166 pym = gPad->YtoAbsPixel(gPad->YtoPad(fY));
167 }
168 Int_t dist = (Int_t)TMath::Sqrt((px-pxm)*(px-pxm) + (py-pym)*(py-pym));
169
170 //marker size = 1 is about 8 pixels
171 Int_t markerRadius = Int_t(4*fMarkerSize);
172 if (dist <= markerRadius) return 0;
173 if (dist > markerRadius+3) return 999;
174 return dist;
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Draw this marker with its current attributes.
179
181{
182 AppendPad(option);
183
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Draw this marker with new coordinates.
188
190{
191 TMarker *newmarker = new TMarker(x, y, 1);
192 TAttMarker::Copy(*newmarker);
193 newmarker->SetBit(kCanDelete);
194 newmarker->AppendPad();
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Execute action corresponding to one event.
199///
200/// This member function is called when a marker is clicked with the locator
201///
202/// If Left button is clicked on a marker, the marker is moved to
203/// a new position when the mouse button is released.
204
206{
207 if (!gPad) return;
208
209 TPoint p;
210 static Int_t pxold, pyold;
211 static Bool_t ndcsav;
212 Double_t dpx, dpy, xp1,yp1;
213 Bool_t opaque = gPad->OpaqueMoving();
214
215 if (!gPad->IsEditable()) return;
216
217 switch (event) {
218
219 case kButton1Down:
220 ndcsav = TestBit(kMarkerNDC);
221 if (!opaque) {
222 gVirtualX->SetTextColor(-1); // invalidate current text color (use xor mode)
223 TAttMarker::Modify(); //Change marker attributes only if necessary
224 }
225 // No break !!!
226
227 case kMouseMotion:
228 pxold = px; pyold = py;
229 gPad->SetCursor(kMove);
230 break;
231
232 case kButton1Motion:
233 p.fX = pxold; p.fY = pyold;
234 if (!opaque) gVirtualX->DrawPolyMarker(1, &p);
235 p.fX = px; p.fY = py;
236 if (!opaque) gVirtualX->DrawPolyMarker(1, &p);
237 pxold = px; pyold = py;
238 if (opaque) {
239 if (ndcsav) this->SetNDC(kFALSE);
240 this->SetX(gPad->PadtoX(gPad->AbsPixeltoX(px)));
241 this->SetY(gPad->PadtoY(gPad->AbsPixeltoY(py)));
242 gPad->ShowGuidelines(this, event, 'i', true);
243 gPad->Modified(kTRUE);
244 gPad->Update();
245 }
246 break;
247
248 case kButton1Up:
249 if (opaque) {
250 if (ndcsav && !this->TestBit(kMarkerNDC)) {
251 this->SetX((fX - gPad->GetX1())/(gPad->GetX2()-gPad->GetX1()));
252 this->SetY((fY - gPad->GetY1())/(gPad->GetY2()-gPad->GetY1()));
253 this->SetNDC();
254 }
255 gPad->ShowGuidelines(this, event);
256 } else {
257 if (TestBit(kMarkerNDC)) {
258 dpx = gPad->GetX2() - gPad->GetX1();
259 dpy = gPad->GetY2() - gPad->GetY1();
260 xp1 = gPad->GetX1();
261 yp1 = gPad->GetY1();
262 fX = (gPad->AbsPixeltoX(pxold)-xp1)/dpx;
263 fY = (gPad->AbsPixeltoY(pyold)-yp1)/dpy;
264 } else {
265 fX = gPad->PadtoX(gPad->AbsPixeltoX(px));
266 fY = gPad->PadtoY(gPad->AbsPixeltoY(py));
267 }
268 gPad->Modified(kTRUE);
269 gPad->Update();
270 gVirtualX->SetTextColor(-1);
271 }
272 break;
273 }
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// List this marker with its attributes.
278
280{
282 printf("Marker X=%f Y=%f marker type=%d\n",fX,fY,fMarkerStyle);
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// Paint this marker with its current attributes.
287
289{
290 if (!gPad) return;
291 if (TestBit(kMarkerNDC)) {
292 Double_t u = gPad->GetX1() + fX*(gPad->GetX2()-gPad->GetX1());
293 Double_t v = gPad->GetY1() + fY*(gPad->GetY2()-gPad->GetY1());
294 PaintMarker(u,v);
295 } else {
296 PaintMarker(gPad->XtoPad(fX),gPad->YtoPad(fY));
297 }
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Draw this marker with new coordinates.
302
304{
305 TAttMarker::Modify(); //Change line attributes only if necessary
306 if (gPad) gPad->PaintPolyMarker(-1,&x,&y,"");
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Draw this marker with new coordinates in NDC.
311
313{
314}
315
316////////////////////////////////////////////////////////////////////////////////
317/// Dump this marker with its attributes.
318
320{
321 printf("Marker X=%f Y=%f",fX,fY);
322 if (GetMarkerColor() != 1) printf(" Color=%d",GetMarkerColor());
323 if (GetMarkerStyle() != 1) printf(" MarkerStyle=%d",GetMarkerStyle());
324 if (GetMarkerSize() != 1) printf(" MarkerSize=%f",GetMarkerSize());
325 printf("\n");
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// Save primitive as a C++ statement(s) on output stream out
330
331void TMarker::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
332{
333 if (gROOT->ClassSaved(TMarker::Class())) {
334 out<<" ";
335 } else {
336 out<<" TMarker *";
337 }
338 out<<"marker = new TMarker("<<fX<<","<<fY<<","<<fMarkerStyle<<");"<<std::endl;
339
340 SaveMarkerAttributes(out,"marker",1,1,1);
341
342 out<<" marker->Draw();"<<std::endl;
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Set NDC mode on if isNDC = kTRUE, off otherwise
347
349{
351 if (isNDC) SetBit(kMarkerNDC);
352}
353
354////////////////////////////////////////////////////////////////////////////////
355/// Stream an object of class TMarker.
356
357void TMarker::Streamer(TBuffer &R__b)
358{
359 if (R__b.IsReading()) {
360 UInt_t R__s, R__c;
361 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
362 if (R__v > 1) {
363 R__b.ReadClassBuffer(TMarker::Class(), this, R__v, R__s, R__c);
364 return;
365 }
366 //====process old versions before automatic schema evolution
367 TObject::Streamer(R__b);
368 TAttMarker::Streamer(R__b);
369 Float_t x,y;
370 R__b >> x; fX = x;
371 R__b >> y; fY = y;
372 //====end of old versions
373
374 } else {
375 R__b.WriteClassBuffer(TMarker::Class(),this);
376 }
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// Return the bounding Box of the Line
381
383{
384 Double_t size = this->GetMarkerSize();
385
386 Rectangle_t BBox;
387 BBox.fX = gPad->XtoPixel(fX)+(Int_t)(2*size);
388 BBox.fY = gPad->YtoPixel(fY)-(Int_t)(2*size);
389 BBox.fWidth = 2*size;
390 BBox.fHeight = 2*size;
391 return (BBox);
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// Return the center of the BoundingBox as TPoint in pixels
396
398{
399 TPoint p;
400 if (!gPad) return (p);
401 p.SetX(gPad->XtoPixel(fX));
402 p.SetY(gPad->YtoPixel(fY));
403 return(p);
404}
405
406////////////////////////////////////////////////////////////////////////////////
407/// Set center of the BoundingBox
408
410{
411 if (!gPad) return;
412 fX = gPad->PixeltoX(p.GetX());
413 fY = gPad->PixeltoY(p.GetY() - gPad->VtoPixel(0));
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Set X coordinate of the center of the BoundingBox
418
420{
421 if (!gPad) return;
422 fX = gPad->PixeltoX(x);
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// Set Y coordinate of the center of the BoundingBox
427
429{
430 if (!gPad) return;
431 fY = gPad->PixeltoY(y - gPad->VtoPixel(0));
432}
433
434////////////////////////////////////////////////////////////////////////////////
435/// Set left hand side of BoundingBox to a value
436/// (resize in x direction on left)
437
439{
440 if (!gPad) return;
441 Double_t size = this->GetMarkerSize();
442 fX = gPad->PixeltoX(x + (Int_t)size);
443}
444
445////////////////////////////////////////////////////////////////////////////////
446/// Set right hand side of BoundingBox to a value
447/// (resize in x direction on right)
448
450{
451 if (!gPad) return;
452 Double_t size = this->GetMarkerSize();
453 fX = gPad->PixeltoX(x - (Int_t)size);
454}
455
456////////////////////////////////////////////////////////////////////////////////
457/// Set top of BoundingBox to a value (resize in y direction on top)
458
460{
461 if (!gPad) return;
462 Double_t size = this->GetMarkerSize();
463 fY = gPad->PixeltoY(y - (Int_t)size - gPad->VtoPixel(0));
464}
465
466////////////////////////////////////////////////////////////////////////////////
467/// Set bottom of BoundingBox to a value
468/// (resize in y direction on bottom)
469
471{
472 if (!gPad) return;
473 Double_t size = this->GetMarkerSize();
474 fY = gPad->PixeltoY(y + (Int_t)size - gPad->VtoPixel(0));
475}
@ kMouseMotion
Definition Buttons.h:23
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kButton1Down
Definition Buttons.h:17
@ kMove
Definition GuiTypes.h:374
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
const Bool_t kFALSE
Definition RtypesCore.h:92
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
#define gROOT
Definition TROOT.h:406
#define gPad
#define gVirtualX
Definition TVirtualX.h:338
#define snprintf
Definition civetweb.c:1540
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
Marker Attributes class.
Definition TAttMarker.h:19
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
virtual void Modify()
Change current marker attributes if necessary.
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:32
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:31
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:33
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:41
Size_t fMarkerSize
Marker size.
Definition TAttMarker.h:24
Style_t fMarkerStyle
Marker style.
Definition TAttMarker.h:23
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:41
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:45
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:46
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Manages Markers.
Definition TMarker.h:22
Double_t fX
X position of marker (left,center,etc..)
Definition TMarker.h:25
virtual void SetX(Double_t x)
Definition TMarker.h:53
virtual void SetBBoxCenter(const TPoint &p)
Set center of the BoundingBox.
Definition TMarker.cxx:409
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TMarker.cxx:348
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition TMarker.cxx:331
TMarker()
Marker default constructor.
Definition TMarker.cxx:37
Double_t fY
Y position of marker (left,center,etc..)
Definition TMarker.h:26
virtual void SetBBoxCenterY(const Int_t y)
Set Y coordinate of the center of the BoundingBox.
Definition TMarker.cxx:428
virtual void SetBBoxX2(const Int_t x)
Set right hand side of BoundingBox to a value (resize in x direction on right)
Definition TMarker.cxx:449
virtual void Print(Option_t *option="") const
Dump this marker with its attributes.
Definition TMarker.cxx:319
virtual void Paint(Option_t *option="")
Paint this marker with its current attributes.
Definition TMarker.cxx:288
virtual void SetY(Double_t y)
Definition TMarker.h:54
virtual void SetBBoxY2(const Int_t y)
Set bottom of BoundingBox to a value (resize in y direction on bottom)
Definition TMarker.cxx:470
virtual Rectangle_t GetBBox()
Return the bounding Box of the Line.
Definition TMarker.cxx:382
virtual void Draw(Option_t *option="")
Draw this marker with its current attributes.
Definition TMarker.cxx:180
void Copy(TObject &marker) const
Copy this marker to marker.
Definition TMarker.cxx:74
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Execute action corresponding to one event.
Definition TMarker.cxx:205
virtual void SetBBoxCenterX(const Int_t x)
Set X coordinate of the center of the BoundingBox.
Definition TMarker.cxx:419
virtual ~TMarker()
Marker default destructor.
Definition TMarker.cxx:57
@ kMarkerNDC
Marker position is in NDC.
Definition TMarker.h:31
virtual void PaintMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:303
virtual TPoint GetBBoxCenter()
Return the center of the BoundingBox as TPoint in pixels.
Definition TMarker.cxx:397
static void DisplayMarkerLineWidths()
Display the table of markers with different line widths and their numbers.
Definition TMarker.cxx:118
virtual void SetBBoxY1(const Int_t y)
Set top of BoundingBox to a value (resize in y direction on top)
Definition TMarker.cxx:459
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Compute distance from point px,py to a marker.
Definition TMarker.cxx:158
virtual void SetBBoxX1(const Int_t x)
Set left hand side of BoundingBox to a value (resize in x direction on left)
Definition TMarker.cxx:438
virtual void ls(Option_t *option="") const
List this marker with its attributes.
Definition TMarker.cxx:279
virtual void DrawMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:189
static void DisplayMarkerTypes()
Display the table of markers with their numbers.
Definition TMarker.cxx:85
virtual void PaintMarkerNDC(Double_t u, Double_t v)
Draw this marker with new coordinates in NDC.
Definition TMarker.cxx:312
Mother of all ROOT objects.
Definition TObject.h:37
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:187
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:107
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:696
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:63
void ResetBit(UInt_t f)
Definition TObject.h:186
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:58
SCoord_t fY
Definition TPoint.h:36
SCoord_t fX
Definition TPoint.h:35
SCoord_t GetY() const
Definition TPoint.h:47
void SetX(SCoord_t x)
Definition TPoint.h:48
void SetY(SCoord_t y)
Definition TPoint.h:49
SCoord_t GetX() const
Definition TPoint.h:46
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2811
Base class for several text objects.
Definition TText.h:22
virtual TText * DrawText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:175
TText * text
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t Sqrt(Double_t x)
Definition TMath.h:691
Rectangle structure (maps to the X11 XRectangle structure)
Definition GuiTypes.h:361
Short_t fX
Definition GuiTypes.h:362
UShort_t fHeight
Definition GuiTypes.h:363
Short_t fY
Definition GuiTypes.h:362
UShort_t fWidth
Definition GuiTypes.h:363