# From user to ndc

There are some function to convert coordinate from pixel / user… coordinate, but I haven’t found a function to convert a user coordinate to an NDC coordinate.

You just need to divide the X (Y) value by the X (Y) maximum value in user coordinates.

No, it’s not so simple:

``````TCanvas c
TH1F h("h", "h", 100, 0, 100)
h.Draw()
TLine l(10,0.6,20,0.6)
l.Draw()
double x1 = 10 / 100.
double x2 = 20 / 100.
TLine l2
l2.DrawLineNDC(x1,0.5,x2,0.5)``````

lines are not aligned

``````{
TCanvas c;
TH1F h("h", "h", 100, 0, 100);
h.Draw();
TLine l(10,0.6,20,0.6);
l.Draw();
printf("%g %g %g\n",xr,x1,x2);
TLine l2;
l2.DrawLineNDC(x1,0.5,x2,0.5);
}``````

[quote=“couet”]``` { TCanvas c; TH1F h("h", "h", 100, 0, 100); h.Draw(); TLine l(10,0.6,20,0.6); l.Draw(); gPad->Update(); Double_t xr = gPad->GetX2()-gPad->GetX1(); double x1 = (10-gPad->GetX1())/ xr; double x2 = (20-gPad->GetX1())/ xr; printf("%g %g %g\n",xr,x1,x2); TLine l2; l2.DrawLineNDC(x1,0.5,x2,0.5); } ```[/quote]

Thank you very much. I think it’s userful to have a set of member functions of TPad to do all the convertions between coordinate system

Good example!

Here is my conversion function abstracted from the code above:

``````double GetNDC(double x) {