Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How would you generate a diagonal line pattern?

Author
Message
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 22nd Aug 2013 21:29 Edited at: 22nd Aug 2013 21:45
Given to you is a 2D area, a 2d line spacing vector, and a 2d line thickness vector.

The area is represented by a 4D vector, containing a left-most, top-most, right and bottom element.






How would you generate the above two patterns with the same algorithm?

I could not find any examples for dummies.... I am unqualified to figure something out efficient without many loops.

I need to work out where the lines stop, where they start, and how many to draw to thicken them if required.

Help would suffice if it just directed me to as to how to determine where to start and stop each line in the pattern. Clipping of the edges is trivial for me; determining the edge of each diagonal line is not.

My mind indicated to me that I aught to spend a few minutes creating a diagonal line pattern for a bar graph.

I think this will come up again later when I add a line generator to my texture generator in my editor; (Post your name and website in any code example if you want your credit for the solution in its generator library)

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd Aug 2013 23:39 Edited at: 22nd Aug 2013 23:44
I'm afraid this was written in dbc, but here you go:




The gluteus-maximus mammary-gland formally known as OBese87.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 22nd Aug 2013 23:48
Before I play with the math, have you thought of doing them horizontally then just rotating them 45 degrees around the centre?

"Get in the Van!" - Van B
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Aug 2013 00:47 Edited at: 23rd Aug 2013 01:32
Graphs is it?
<edited 23:30>

That turned out pretty sweet!

Image of the output:




The gluteus-maximus mammary-gland formally known as OBese87.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 23rd Aug 2013 02:18 Edited at: 23rd Aug 2013 02:54
Here's a maths version that clips to the size of the rect (square), this could be edited to accept any rect really but just wrote this quick.





Code needs alittle refining, I'd just use Advanced2D for this and extend the lines and just clip them cause it's far quicker than working out where they need to be.

Edit: Advanced2D version, better plus lines are evenly positioned


"Get in the Van!" - Van B
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 23rd Aug 2013 02:29 Edited at: 23rd Aug 2013 02:30
The way mine works is say we had point running some where along the top of the square which would be it's start point, the distance from the left top point of the square to that point would be the end point on the left side of the square.

So say dot StartPoint):

x = 20
y = the top position of the square

It's EndPoint) would be:

x = the left position of the square
y = StartPoint.x

If we join these up we get a perfect 45 degree line, then we just add lines next to it to thicken it. When the StartPoint.x goes beyond the right position of the square, we flip the calculation to work out the rest which now start on the right side and ends on the bottom.

"Get in the Van!" - Van B
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 23rd Aug 2013 03:10 Edited at: 23rd Aug 2013 03:11
Wow, how inspirational. I must get some rest now, but will definitely take a thorough look at your methods tomorrow. They all seem good for a variety of needs.

I should mention that I do have Advance2D, Matrix1 and Image Kit installed.

I was thinking of using shaders, but did not think it was necessary for small little bar graphs and UI patterns.

@Sasuke

You cracked it. I didn't think of coordinate wrapping.

a2SetClip is already being used to limit the drawing area on a user-interface panel; I am guessing the removal of it in your method wouldn't spoil it. I can cover jagged edges with a border if this is the case.

@Libervurto

That phase graph looks really cool; it inspires thought regarding special effects. Is there a known reason for the lines not being at 45 degrees?

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Aug 2013 08:49
Quote: "Is there a known reason for the lines not being at 45 degrees?"

Oops! That was from unintentional stretching when scaling it up.

Got a much higher resolution to run at a decent speed. Here are some different patterns on the same graph →



The gluteus-maximus mammary-gland formally known as OBese87.
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 23rd Aug 2013 10:44
It's just a sliding window, so we'd render a single row of the pattern that's at least twice the size of the output, then copy offset strips from it. Bellow it's doing it in strings (PB), couldn't be bothered writing it in DB/DBpro.



in DBPRO, what i'd most likely do is create the target surface, then render the single scan line pattern into a mem block, then copy the displaced strips from that mem block, directly to the locked surface. Which avoid most of the call overhead from poking the memblock.

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 23rd Aug 2013 19:03
@Kevin

I see your point Kevin; I think that could work rather well with a generated sprite representing a pixel row, having its UV coordinates adjusted as it is painted down its designated area.

@Libervurto

Nice one; I will be using that for transition effects. Points along the line need not be pixels, they could be images and particle effects.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Aug 2013 21:00 Edited at: 23rd Aug 2013 21:06
Quote: "render the single scan line pattern into a mem block, then copy the displaced strips from that mem block, directly to the locked surface."

I am thinking the same thing, but I was over-thinking it and trying to work out the formulas to generate all of the patterns in my bitwise solution.

When I was thinking about that it occurred to me that it would be useful to know the most- and least-significant set bits. Is there a better way than:


@Chris — You were right about the "this"/"is"/"in"/etc. variable name prefixes. I am using them all the time now, they are nice and concise.



Formerly OBese87.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th Aug 2013 11:10
I wanted to try doing the same with pie charts, but I'm really struggling to work out the maths. If I have a circle of radius r, which I draw in strips from x= (originX - r) to (originX + r), how do I work out the length of each slice of the circle at point x?



Formerly OBese87.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 24th Aug 2013 14:06 Edited at: 24th Aug 2013 14:13
Guess number one..

Erm. Perhaps it might work if the Y position is used to iterate from 0 to 180. From the top to the very bottom of the circle. And this iterator is used as a SIN parameter which multiplies the radius.

That could work for the perimeter; as for the slices, you'd need some kind of intersection along the two lines stemming from the origin. I don't know if I am being clear enough.

Guess number two:

Perhaps the stripes and the pie chart could be delt with seperately; and the stripes could be used as a pixel mask; or perhaps the stripes could be used as a texture source for the pie chart, where each point is given a UV coordinate.

Quote: "@Chris — You were right about the "this"/"is"/"in"/etc. variable name prefixes. I am using them all the time now, they are nice and concise. "


It feels justified doesn't it; and it provides more variations of related variable names.

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 26th Aug 2013 18:35 Edited at: 26th Aug 2013 18:39
Well to start off I implemented Sasuke's solution into a bar graph using a watered down version of my design functions However this drew my attention to the fact that the number of diagonal lines needs to be worked out for each call to the drawing function.

Requires A2D and Matrix1


Next I will need to figure out how to work the line count for the given length.

The attached image indicates that I may need to resort to baking a stripe image at the maximum width, then clip that; unless there is a better way.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Aug 2013 20:41 Edited at: 26th Aug 2013 20:58
Pic →




Formerly OBese87.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 26th Aug 2013 21:37
Wow that looks pretty smart and concise!

For some reason your text editor is copying single & symbols into your clipboard, instead of &&.

So, what figure represents the bar height and stripe thickness? If I change the 20 in the for loop it messes it up.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Aug 2013 22:38
Quote: "For some reason your text editor is copying single & symbols into your clipboard, instead of &&."

Ah, that is a nasty bug called DBC.

Here is an image of all the patterns produced by the equation: (x+y)&&p, where p is the pattern number. →
The value of p could go beyond 63, but at this scale the patterns repeat; the patterns above 63 are not actually the same but they have the same top corner so the rest of the pattern would not be seen in these small boxes. If you look closely, you can see that if you took an even smaller box the patterns would repeat themselves even sooner.



Formerly OBese87.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Aug 2013 22:40 Edited at: 26th Aug 2013 23:22
I don't totally understand the bitwise patterns, but you can get some really funky stuff by adjusting the equation. Here is (x&&y)&&p, which produces a Sierpinski triangle →
You can really see here how the patterns from different bits are laid on top of each other.



Formerly OBese87.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st Aug 2013 18:38
I'm determined to do a pie-chart. This is the best so far →



Formerly OBese87.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st Aug 2013 20:42
Drawing sectors →



Formerly OBese87.
Silverman
19
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 1st Sep 2013 10:34
Hi Libervurto,

it seems that you like the boolean.
Here is my entire library(XOR, NXOR(also EQV), bit manipulation, ...) of Boolean logic, maybe it will serve you to find new patterns.



enjoy!

DirectX 9.0c (February 2010)/ DBClassic v1.20
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Sep 2013 14:51 Edited at: 6th Sep 2013 16:22
Libervurto, (used to be obese?) it's your fault I got out of bed before 8am this morning. I was reading this thread from my laptop when it made me want to try pie charts.

Ok, pie charts will come later. I'm having fun with just the bars.

Here's my animated loading bar:


Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Sep 2013 19:15 Edited at: 6th Sep 2013 19:18
Modified code above to use a rounded box instead. Clipping routine adapted from Neuro Fuzzy's code.



It's pretty slow, but I believe that's due to my drawing routines.



Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 6th Sep 2013 22:20 Edited at: 6th Sep 2013 22:23
My animated bar


Had any luck with the piecharts?



Formerly OBese87.
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 6th Sep 2013 22:40
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 7th Sep 2013 02:41
Bloody-hell, you guys are way ahead of me, I am still working on my rectangular bar graphs!

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Sep 2013 07:18
Quote: "Had any luck with the piecharts?"

Didn't start yet.

Your code is way shorter!

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Sep 2013 15:45
Quote: "Your code is way shorter!"

That is what's nice about bitwising the coordinates, the whole screen automatically becomes a pattern and all you have do is pick the pixels you want.


Formerly OBese87.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Sep 2013 18:16
That's what I'm trying to figure out. I'm not getting how this:

if (x+y+scroll) && fillPattern then dot x,y

Is making a diagonal pattern.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Sep 2013 18:40
Quote: "That's what I'm trying to figure out. I'm not getting how this: if (x+y+scroll) && fillPattern then dot x,y Is making a diagonal pattern."

In this case fillPattern = 8 — or for practical purposes "bit 3 is on" — that means if the result of x+y also has its "bit 3 on" the coordinate x,y is part of the bit pattern and will be coloured.
If we count from 0, bit three will alternate from being off for 8 iterations and then on for 8 iterations.
Therefore, moving in a straight line either horizontally or vertically produces a dotted line 8 pixels thick.
Offsetting that dotted line by one pixel in the opposing axis shifts the dotted line up/down one pixel, since we are adding x+y. This two-dimensional offset naturally produces a diagonal pattern. The scroll variable simply offsets the pattern even more, I think of it as tricking the pattern into thinking we are moving across the screen when we aren't.


Formerly OBese87.

Login to post a reply

Server time is: 2026-07-06 16:49:32
Your offset time is: 2026-07-06 16:49:32