So, I want something that calculates the points between two other points. I tried this [URL=http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm]"Bresenham's line algorithm"[/URL] but it didn't work out. Here's the function from it (C# with the XNA framework):
[csharp]
public List<Vector2> GetPixelsInLine(Vector2 p1, Vector2 p2)
{
List<Vector2> ret = new List<Vector2>();
float deltax = p2.X - p1.X;
float deltay = p2.Y - p1.Y;
float error = 0;
float deltaerr = deltay / deltax;
float y = p1.Y;
for (float x = p1.X; x < p2.X; x++)
{
ret.Add(new Vector2(x, y));
error = error + deltaerr;
if (Math.Abs(error) >= 0.5)
{
y = y + 1;
error = error - 1;
}
}
return ret;
}
[/csharp]
Please help me!
It... Didn't work?
What do you mean it didn't work?
Also, couldn't you just [url=http://en.wikipedia.org/wiki/Linear_equation#Two-point_form]use the line equation[/url] and round y correctly by doing something like Math.Floor( y + 0.5 )?
[editline]11th November 2010[/editline]
It should be basic algebra but I can't tell you why it doesn't work if it doesn't 'cause I'm gonna go to sleep :v:
[QUOTE=esalaka;25999169]It... Didn't work?
What do you mean it didn't work?
Also, couldn't you just [url=http://en.wikipedia.org/wiki/Linear_equation#Two-point_form]use the line equation[/url] and round y correctly by doing something like Math.Floor( y + 0.5 )?
[editline]11th November 2010[/editline]
It should be basic algebra but I can't tell you why it doesn't work if it doesn't 'cause I'm gonna go to sleep :v:[/QUOTE]
Only works when dx > dy. The Bresenham algorithm switches x and y if that is not the case.
You could easily get a specific point or set number of points between them by using Vector2.Lerp():
[code]
List<Vector2> pointsBetweenPoints(Vector2 p1, Vector2 p2)
{
List<Vector2> points = new List<Vector2>(100);
for(float i = 0; i <= 1; i += 0.01)
{
points.Add(Vector2.Lerp(p1, p2, i);
}
return points;
}
[/code]
What's this for by the way? If it's to move something in a line there are nicer ways :v:
Probably line drawing.
Here's my implementation (essentially identical to the wikipedia pseudocode but translated into C++. It works 100%) of bresenhams (which I used in my [B][url="http://jallenbah.co.uk/mousetracker.php"]Mouse Tracker[/url][/B]) with all of the drawing/library specific stuff edited out:
[cpp]void Line(int x0, int y0, int x1, int y1)
{
bool steep = abs(y1 - y0) > abs(x1 - x0);
if(steep)
{
Swap(x0, y0); // swap simply swaps the values of the parameters
Swap(x1, y1);
}
if(x0 > x1)
{
Swap(x0, x1);
Swap(y0, y1);
}
int deltax = x1 - x0;
int deltay = abs(y1 - y0);
float error = 0;
float deltaerr = float(deltay) / deltax;
int ystep;
int y = y0;
if(y0 < y1) ystep = 1; else ystep = -1;
for(int x = x0; x < x1; x++)
{
if(steep)
{
// the pixel at Vector(y,x) is on the line
}
else
{
// the pixel at Vector(x,y) is on the line
}
error = error + deltaerr;
if(error >= 0.5)
{
y = y + ystep;
error = error - 1.0;
}
}
}[/cpp]
Hope this could be of some help.
[QUOTE=Matthew0505;26015119]The Wikipedia page has an integer version you know. Using floats is useless since you can't split pixels.[/QUOTE]
Actually you can on a LCD screen. It's called subpixel rendering.
[QUOTE=Matthew0505;26027917]Still not something you would do with floats[/QUOTE]
Why not? SFML does that AFAIK.
[QUOTE=Matthew0505;26039916]Because you wouldn't use an inaccurate float for precision subpixel rendering[/QUOTE]
Subpixel rendering is just fuzzing colours together anyway, so it's hardly accurate to begin with.
[QUOTE=Siemens;26040651]Subpixel rendering is just fuzzing colours together anyway, so it's hardly accurate to begin with.[/QUOTE]
No.
Subpixel rendering is the process of using the R, G and B components of a pixel on a LCD screen to draw things in triple the horizontal (x-axis) definition by essentially treating each component as a gray pixel. That's why your fonts look so smooth nowadays!
Last I checked R, G and B are colours bro.
Yes, but the eye treats an RGB block and a GBR block as both being black, even though one is shifted one-third of a pixel. There is always some colour fringing though.
[QUOTE=shill le 2nd;26048501]Yes, but the eye treats an RGB block and a GBR block as both being black, even though one is shifted one-third of a pixel. There is always some colour fringing though.[/QUOTE]
I should clarify.
What I meant by 'colour fuzzing' is that the techniques used to achieve subpixel rendering basically treat each pixel's colour components as a unit that's able to be used to gain a greater horizontal resolution.
So there's really nothing wrong with using floats to do subpixel rendering because subpixel rendering isn't the most accurate technique anyway.
Sorry, you need to Log In to post a reply to this thread.