Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » Draw, Display, Images, Bitmaps, Icons » Single Pixel Wide Straight Lines
Single Pixel Wide Straight Lines [message #59012] Fri, 14 October 2022 16:15 Go to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
So if I use Draw directly from the Paint function parameter I can draw Straight lines that are a single pixel wide.

If I use PaintingPainter, ImageBuffer BufferPainter combo, or DrawPainter Nearly all lines that are supposed to be single pixel wide are two pixels with fading effects applied to them. this is with MODE_ANTIALIASED, MODE_NOAA, MODE_SUBPIXEL as well. The reason why I say nearly all is because in some cases when I do a grid some lines are actually single lines but most are not.

The reason I can;t just use Draw is because I need the ability to draw in the same place twice and have them show both sort of mixed together.

Is there a way to draw single pixel wide straight lines or is there a way to paint pixels in an array or buffer or something so I can do that my self.

Re: Single Pixel Wide Straight Lines [message #59013 is a reply to message #59012] Fri, 14 October 2022 16:19 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
This is an example of what I mean about it being 2 pixels when its supposed to be one.
Re: Single Pixel Wide Straight Lines [message #59014 is a reply to message #59012] Fri, 14 October 2022 19:04 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
Fist Picture is an example where it sometimes is a single pixel line but most the time 2

Second picture is where I use Draw directly from paint and single pixel lines work but as you see no green at the bottom to do it being not blended in.

Third picture is the same as second picture just using
ImageBuffer ib(sz);
BufferPainter sw(ib, MODE_ANTIALIASED);
w.DrawImage(sz, ib);

index.php?t=getfile&id=6678&private=0

index.php?t=getfile&id=6680&private=0

index.php?t=getfile&id=6679&private=0
Re: Single Pixel Wide Straight Lines [message #59015 is a reply to message #59012] Fri, 14 October 2022 19:55 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi devilsclaw,

It looks like you're actually drawing single pixel lines that do not "fit in the pixel". The engine actually draws half of it into one pixel and the other half to the adjacent one... This is not usually a problem, but it produces ugly results for vertical and horizontal lines. Try moving the horizontal lines 0.5 pixel up or down and similarly vertical lines 0.5px left or right Wink

The only weird thing is that it does produce fuzzy effect even with NO_AA. I'd expect that without antialiasing, the coordinates would be rounded to the nearest pixel.

Best regards,
Honza
Re: Single Pixel Wide Straight Lines [message #59016 is a reply to message #59012] Fri, 14 October 2022 20:05 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
I am using DrawLine which only allows integers to draw so how can it be half a pixel at that point ?.

I can try using Move + Line again and shift pixels.

The throughput is using Move + Line which would explain the half pixel thing there but none of the others are.
Re: Single Pixel Wide Straight Lines [message #59018 is a reply to message #59012] Fri, 14 October 2022 20:46 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
So I tried what you suggested moved it by 0.5 of a pix which moved it from 10 to 10.5 and then the line was a single pixel. But that is not possible to do with DrawLine since its integer. This to me feels like a bug that you have to be half a pixel off to get a pixel line width.
Re: Single Pixel Wide Straight Lines [message #59019 is a reply to message #59012] Fri, 14 October 2022 23:33 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
So I figured out what is going on. Lines are painted from the center / origin of the point so if you say line is at y coord 10 and have a line width of 3 it will be 1.5 pixels below and above instead of starting the line at 10 and having 3 lines going down from the starting point.

This means all odd line sizes as in 1,2,5,7,9 etc will be the incorrect size with any integer based drawing on everything except Draw its self it seems. I believe this is the wrong way to draw a line it should not be center radial.

This code example shows it working and clean lines

void Paint(Draw &w) {
  Size sz = GetSize();
  ImageBuffer ib(sz);
  BufferPainter sw(ib, MODE_ANTIALIASED);
  
  sw.DrawRect(sz, Black());
  
  int pos_y = 0;
  for(int stroke = 1; true; stroke += 1) {
    double center_of_line = stroke / 2.0;
    sw.Move( 0, pos_y + center_of_line).Line(sz.cx, pos_y + center_of_line).Stroke(stroke, Red());
    pos_y += (stroke * 2);
    if(pos_y >= sz.cy) {
     break;
    }
  }
}


index.php?t=getfile&id=6681&private=0

[Updated on: Fri, 14 October 2022 23:35]

Report message to a moderator

Re: Single Pixel Wide Straight Lines [message #59020 is a reply to message #59012] Fri, 14 October 2022 23:58 Go to previous messageGo to next message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
To fix the below I ended up doing this.

void DrawLine(BufferPainter* bp, int x1, int y1, int x2, int y2, double stroke, Color color) {
  double center_of_line_x = 0;
  double center_of_line_y = 0;
  if(x1 == x2) {
    center_of_line_x = stroke / 2.0;
  }
  if(y1 == y2) {
    center_of_line_y = stroke / 2.0;
  }
  bp->Move(x1 + center_of_line_x, y1 + center_of_line_y).Line(x2 + center_of_line_x, y2 + center_of_line_y).Stroke(stroke, color);
}


index.php?t=getfile&id=6682&private=0
Re: Single Pixel Wide Straight Lines [message #59021 is a reply to message #59012] Sat, 15 October 2022 09:23 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1118
Registered: August 2007
Senior Contributor
Quote:
is there a way to paint pixels in an array or buffer or something so I can do that my self.


Yes. ImageBuffer allows direct access to its RGBA buffer.

e.g:
	void Paint(Draw& w) override
	{
		Size sz = GetSize();
		w.DrawRect(sz, Black());
		ImageBuffer canvas(sz);
		
		for(int y = 0; y < sz.cy; y++) {
			for(int x = 0; x < sz.cx; x++) {
				if((y % 16 == 0) || (x % 16 == 0))
					canvas[y][x] = White();
			}
		}
		w.DrawImage(0, 0, canvas);
		
	}


is possible.

Best regards,
Oblivion


Re: Single Pixel Wide Straight Lines [message #59331 is a reply to message #59021] Sat, 17 December 2022 08:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14059
Registered: November 2005
Ultimate Member
Obvious trick here is to do Translate(0.5, 0.5) at the beginning.
Re: Single Pixel Wide Straight Lines [message #59337 is a reply to message #59331] Sat, 17 December 2022 16:19 Go to previous message
devilsclaw is currently offline  devilsclaw
Messages: 72
Registered: August 2022
Member
Actually that would not work as you think since the problem is that that lines are drawn from there radial center and if you just shift everything by .5 then the even number lines will be messed up like the current odd line are. sure if the line is old then shift by 0.5 might be easier.
Previous Topic: Text painting issue with 'const char*'
Next Topic: Crash when loading some jpeg files
Goto Forum:
  


Current Time: Sun Oct 13 03:10:46 CEST 2024

Total time taken to generate the page: 0.03359 seconds