Drawing a line code

Joel joelcnz at gmail.com
Sun Nov 6 11:22:26 UTC 2022


I found some code on the net but haven't been able to get it 
working properly. I trying to draw with mouse (any direction).

```d
	void drawLine(Dot s, Dot e) {
		auto d=s;

/+
		// import std.algorithm : swap;

		if (s.pos.X>e.pos.X) {
			int ox=s.pos.Xi;
			s.pos=Point(e.pos.X,s.pos.Y);
			e.pos=Point(ox,e.pos.Y);
			// swap(s.pos.X, e.pos.X);
		}

		if (s.pos.Y>e.pos.Y) {
			int oy=s.pos.Yi;
			s.pos=Point(s.pos.X,e.pos.Y);
			e.pos=Point(e.pos.X,oy);
			// swap(s.pos.Y, e.pos.Y);
		}
+/
		int x0=s.pos.Xi, x1=e.pos.Xi, y0=s.pos.Yi, y1=e.pos.Yi;
		int dy, dx, incrE, incrNE, dt,x,y;

/+
		import std.algorithm : swap;
		if (x0>x1)
			swap(x0,x1);
		if (y0>y1)
			swap(y0,y1);
+/
		dx = x1 - x0;
		dy = y1 - y0;
		dt = 2 * (dy - dx);
		incrE = 2*dy;
		incrNE = 2*(dy - dx);
		x = x0;
		y = y0;

		d.setPos(s.pos);
		drawDot(d);

		while(x < x1)
		{
			if (dt <= 0)
			{
				dt += incrE;
				x++;
			}
			else
			{
				dt += incrNE;
				x++;
				y++;
			}
			d.setPos(Point(x,y));
			drawDot(d);
		}
	} // drawLine
```



More information about the Digitalmars-d-learn mailing list