Draw lines with sharp ends

I am drawing lines using GfxLineTo. Depending on the max and min price within a pane, I set line width using GfxSelectPen to draws lines of width more than 1 pixel. But lines of width more than 1 pixel show rounded ends which overshoot the first as well as last 'x' co-ordinate. I would like to draw lines with sharp ends just like a rectangle. Is there any parameter which makes it possible?

Example of my code:

GfxSelectPen(color, lineWidth); // Linewidth can be 3 or even 5 in some cases.
GfxMoveTo(x1, y);
GfxLineTo(x2, y);

If rounded ends are such a big problem for you, then just draw a rectangle. Instead of:

width = 6;
GfxSelectPen(colorWhite, width);
GfxMoveTo(100, 100);
GfxLineTo(300, 100);

line1

Use:

width = 6;
GfxFillSolidRect(100, 100 - width/2, 300, 100 + width/2, colorWhite); 

line2

1 Like

Thanks @Milosz. That works for me. Will draw a rectangle instead of lines.

By the way, rounded ends overshooting the calculated co-ordinates was a bigger issue than rounded ends themselves.

This is how lines in Windows GDI work. Default value for pens is PS_ENDCAP_ROUND
https://msdn.microsoft.com/en-us/library/windows/desktop/dd162705(v=vs.85).aspx

So the ends of fat lines are ended with circle with circle center at the beginning and end of the line (round end cap). This is the "overshoot". It works as if you drawn the line with fat marker pen manually on piece of paper. Fat marker makes fat line and fat ends. This is done to make sure that lines with multiple segments have smooth joints.

Other end caps are not supported at the moment but it can be added if lots of people need that.

2 Likes

In version 6.30 GfxSelectPen supports now geometric pens so you can have flat ends:

Version( 6.30 );

PS_ENDCAP_FLAT = 0x200;
PS_ENDCAP_SQUARE = 0x100;

GfxFillSolidRect( 100, 80, 200, 260, colorGreen );

// flat end cap
GfxSelectPen( colorRed, 10, PS_ENDCAP_FLAT );
GfxMoveTo( 100, 100 );
GfxLineTo( 200, 200 );

// square end cap
GfxSelectPen( colorRed, 10, PS_ENDCAP_SQUARE );
GfxMoveTo( 100, 120 );
GfxLineTo( 200, 220 );

// round end cap (default)
GfxSelectPen( colorRed, 10 );
GfxMoveTo( 100, 140 );
GfxLineTo( 200, 240 );
4 Likes

thank you very much for this - very helpful