Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 19 Oct 2013 02:34 PM |
This is an excerpt from a graphics API I've been working on for the past few weeks: Lines and anti-aliased lines. Enjoy.
rudiject = { properties = { stroke = Color3.new(0, 0, 0), fill = Color3.new(0, 0, 0), gframe = script.Parent.ExemplarFrame, cyclic = 0 }, classes = { ['point']= function(frame, x1, y1, c, x2, y2) if (frame) then g = Instance.new('TextLabel', frame) g.Name = 'pt' g.Text = '' g.BorderColor3 = rudiject.properties.stroke g.BackgroundColor3 = rudiject.properties.stroke g.BackgroundTransparency = c or 0 g.Position = UDim2.new(0, x1, 0, y1) g.Size = x2 and y2 and UDim2.new(0, x2, 0, y2) or UDim2.new(0, 1, 0, 1) end end,
['line'] = function(frame, x1, y1, x2, y2, name) -- Based upon Bresenham's Line Algorithm. -- Non-optimized. name = name or 'Line'..rudiject.properties.cyclic Instance.new('Frame', frame).Name = name deltax, deltay = 2*(x2 - x1), 2*(y2 - y1) ix, iy = deltax/math.abs(deltax), deltay/math.abs(deltay) rudiject.classes.point(frame[name], x1, y1) if (deltax >= deltay) then error = deltay - deltax / 2 while x1 ~= x2 do if ((error >= 0) and ((error ~= 0) or (ix > 0))) then error = error - deltax y1 = y1 + iy end error = error + deltay x1 = x1 + ix rudiject.classes.point(frame[name], x1, y1) end else error = deltax - deltay / 2 while y1 ~= y2 do if (error >= 0) and ((error ~= 0) or (iy > 0)) then error = error - deltay x1 = x1 + ix end error = error + deltax y1 = y1 + iy rudiject.classes.point(frame[name], x1, y1) end end end, ['aline']= function(frame, x1, y1, x2, y2, name) -- Based upon Xiaolin Wu's line algorithm. -- Non-optimized. name = name or 'Line'..rudiject.properties.cyclic Instance.new('Frame', frame).Name = name steep = math.abs(y2 - y1) > math.abs(x2 - x1) eqlty = x1 > x2 x1, x2, y1, y2 = steep and y1 or x1, steep and y2 or x2, steep and x1 or y1, steep and x2 or y2 x1, x2, y1, y2 = eqlty and x2 or x1, eqlty and x1 or x2, eqlty and y2 or y1, eqlty and y1 or y2 deltax = x2 - x1 deltay = y2 - y1 gradient = deltay/deltax xend = rd(x1) yend = y1 + gradient * (xend - x1) xgap = rf(x1 + 0.5) xpxl1= xend ypxl1= ip(yend) if (steep) then rudiject.classes.point(frame[name], ypxl1, xpxl1, rf(yend) * xgap) rudiject.classes.point(frame[name], ypxl1+1, xpxl1, fp(yend) * xgap) print(xpxl1, ypxl1) else rudiject.classes.point(frame[name], xpxl1, ypxl1 , rf(yend) * xgap) rudiject.classes.point(frame[name], xpxl1, ypxl1+1, fp(yend) * xgap) print(xpxl1, ypxl1) end intery = yend + gradient xend = rd(x2) yend = y1 + gradient * (xend - x1) xgap = rf(x2 + 0.5) xpxl2= xend ypxl2= ip(yend) if (steep) then rudiject.classes.point(frame[name], ypxl2, xpxl2, rf(yend) * xgap * 2) rudiject.classes.point(frame[name], ypxl2+1, xpxl2, fp(yend) * xgap * 2) else rudiject.classes.point(frame[name], xpxl2, ypxl2 , rf(yend) * xgap * 2) rudiject.classes.point(frame[name], xpxl2, ypxl2+1, fp(yend) * xgap * 2) end for i=xpxl1+1, xpxl2-1 do if (steep) then rudiject.classes.point(frame[name], ip(intery), i, rf(intery) * .5) rudiject.classes.point(frame[name], ip(intery)+1, i, fp(intery) * .5) else rudiject.classes.point(frame[name], i, ip(intery), rf(intery)^.5) rudiject.classes.point(frame[name], i, ip(intery)+1, fp(intery)^.5) end intery = intery + gradient end end } } |
|
|
| Report Abuse |
|
|
bohdan77
|
  |
| Joined: 10 Aug 2008 |
| Total Posts: 7944 |
|
| |
|
Vermis
|
  |
| Joined: 02 Jan 2013 |
| Total Posts: 11501 |
|
| |
|
YEGGOR
|
  |
| Joined: 02 Apr 2013 |
| Total Posts: 1150 |
|
|
| 19 Oct 2013 03:12 PM |
| i did this with 100 lines of code before :p |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 19 Oct 2013 03:17 PM |
@bohdan, will do. @yeggor, anti-aliased and aliased? Using O(log n) algorithms? It would probably be shorter if it were outside of the API and disassociated with all of the object-oriented nonsense. |
|
|
| Report Abuse |
|
|
YEGGOR
|
  |
| Joined: 02 Apr 2013 |
| Total Posts: 1150 |
|
|
| 19 Oct 2013 03:25 PM |
spacing = 2 --to make dotted lines if you like
function dir(v1, v2) return (v2 - v1).unit end
function createnewline(v1, v2) local direction = dir(v1,v2) local magnitude = (v2 - v1) local frame = Instance.new("Frame") for i = 0, magnitude, spacing do -- line local a = Instance.new("TextLabel", frame) a.Size = UDim2.new(0,2,0,2) a.Position = direction * i end for i = 0, magnitude, spacing do -- failure aa local aa = Instance.new("TextLabel", frame) aa.Size = UDim2.new(0,4,0,4) aa.Position = direction * i - UDim2.new(0,1,0,1) end end
i think thats less than 100 lines actually |
|
|
| Report Abuse |
|
|
YEGGOR
|
  |
| Joined: 02 Apr 2013 |
| Total Posts: 1150 |
|
|
| 19 Oct 2013 03:25 PM |
v1 and v2 are Vector2 values
also i messed up on the position thing now that i think of it |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
| |
|
Davidii
|
  |
| Joined: 17 Jul 2008 |
| Total Posts: 1282 |
|
|
| 19 Oct 2013 04:28 PM |
| Object-oriented nonsense? The only sense in programming is object oriented! |
|
|
| Report Abuse |
|
|
|
| 19 Oct 2013 08:46 PM |
@Davidii: I have honestly gotten used to more abstract ways of thinking. Sometimes object oriented programming is a good idea, but often it is just plain wrong.
It would also explain why I like assembly more than Java. Hmm. |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 19 Oct 2013 09:20 PM |
| Assembly isn't procedural though? |
|
|
| Report Abuse |
|
|
| |
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 19 Oct 2013 11:48 PM |
| hahaha im o(n) time thats funny i laughed |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
| |
|
|
| 20 Oct 2013 02:48 PM |
| Bressenham can also be used for anti-aliasing, you just don't realize it... |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
| |
|
|
| 20 Oct 2013 02:59 PM |
So, [x, y]. You'll have 4 pixels with these following configurations. [math.floor(x), y].opacity = x - math.floor(x) [math.floor(x)+1, y].opacity = math.floor(x)+1-x
In bressenham, one is always an integer and another may be an integer or a floating point(with this modification) |
|
|
| Report Abuse |
|
|
|
| 20 Oct 2013 03:01 PM |
Not opacity, transparency. 2 pixels*
Let me give you an example in which [1.66, 1]
[1, 1].transparency = 1.66-1 = 0.66 [2, 1].transparency = 2-1.66 = 0.33 |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 20 Oct 2013 03:15 PM |
... That's not anti-aliasing. |
|
|
| Report Abuse |
|
|
|
| 20 Oct 2013 03:20 PM |
| Eh, I believe it has the same effect considering that I tested it on MS Paint. |
|
|
| Report Abuse |
|
|
|
| 20 Oct 2013 03:23 PM |
does it work with rotatoe lines?
that's all I want to know. |
|
|
| Report Abuse |
|
|
|
| 20 Oct 2013 03:24 PM |
| It does rotate because -cough- antialiasing -cough- |
|
|
| Report Abuse |
|
|
|
| 20 Oct 2013 04:03 PM |
| Dude thats not anti Aliasing.. |
|
|
| Report Abuse |
|
|