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