Re: Graphical progressive fill

2022-12-11 Thread Siarhei Siamashka via Digitalmars-d-learn
On Monday, 12 December 2022 at 06:02:27 UTC, Ferhat Kurtulmuş 
wrote:

https://rosettacode.org/wiki/Bitmap/Flood_fill


The https://rosettacode.org/wiki/Bitmap/Flood_fill#D looks like a 
DFS implementation. The end result is the same, but the order in 
which the pixels to fill are reached is different. My 
understanding is that the requested "progressive fill" and "not 
all at once but building up" means that some sort of animation is 
needed with multiple frames showing how the area is getting 
gradually filled.


Here's a better implementation of my BFS code:
```D
import std;

struct point { int x, y; }

void show_grid(char[][] grid) {
  foreach (ref row ; grid)
writeln(row);
  writeln;
}

void animated_fill(char[][] grid, point[] starting_points) {
  auto height = grid.length;
  if (height == 0)
return;
  auto width = grid[0].length;

  struct xpoint { int x, y, dist_from_start; }
  auto queue = uninitializedArray!(xpoint[])(width * height);
  size_t start, end;

  foreach (p ; starting_points) {
if (grid[p.y][p.x] == '.') {
  queue[end++] = xpoint(p.x, p.y, 0);
  grid[p.y][p.x] = '#';
}
  }

  int current_dist = -1;
  while (start < end) {
  auto p = queue[start++];

  if (p.dist_from_start > current_dist) {
show_grid(grid);
current_dist = p.dist_from_start;
  }

  if (p.y + 1 < height && grid[p.y + 1][p.x] == '.') {
queue[end++] = xpoint(p.x, p.y + 1, p.dist_from_start + 
1);

grid[p.y + 1][p.x] = '#';
  }
  if (p.y - 1 >= 0 && grid[p.y - 1][p.x] == '.') {
queue[end++] = xpoint(p.x, p.y - 1, p.dist_from_start + 
1);

grid[p.y - 1][p.x] = '#';
  }
  if (p.x + 1 < width && grid[p.y][p.x + 1] == '.') {
queue[end++] = xpoint(p.x + 1, p.y, p.dist_from_start + 
1);

grid[p.y][p.x + 1] = '#';
  }
  if (p.x - 1 >= 0 && grid[p.y][p.x - 1] == '.') {
queue[end++] = xpoint(p.x - 1, p.y, p.dist_from_start + 
1);

grid[p.y][p.x - 1] = '#';
  }
  }
}

void main() {
  auto grid = ["@.".dup,
   "..".dup,
   "..".dup];
  auto height = grid.length.to!int;
  auto width = grid[0].length.to!int;

  const number_of_starting_points = 2;
  auto random_points = new point[](number_of_starting_points);
  foreach (ref p ; random_points)
p = point(uniform(0, width), uniform(0, height));

  animated_fill(grid, random_points);
}
```

And here's a possible result with a small grid (the "@" cells are 
acting as "walls"):

```
...#@.
..
...#..

..##@.
...#..
..###.

.###@.
..###.
...#.#

@...#.
.#####
..

@..###
##
.#

@.
##
##

@#
##
##
```



Re: Graphical progressive fill

2022-12-11 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 11 December 2022 at 06:50:44 UTC, Joel wrote:
I've been trying to fill in areas with a colour but can't work 
it out. I want something like the effect where it fills with 
diamonds. Not all at once but building up in the main program 
loop.



#  #
#  #
#  #  #
#  #
#  #



#  #
#  #  #
#   ####
#  #  #
#  #



## #
# ####
#  # #
# ###   #
## #



https://rosettacode.org/wiki/Bitmap/Flood_fill


Re: Graphical progressive fill

2022-12-11 Thread Siarhei Siamashka via Digitalmars-d-learn

On Sunday, 11 December 2022 at 06:50:44 UTC, Joel wrote:
I've been trying to fill in areas with a colour but can't work 
it out. I want something like the effect where it fills with 
diamonds. Not all at once but building up in the main program 
loop.


I'm not sure if I understood the question correctly, but maybe 
https://en.wikipedia.org/wiki/Breadth-first_search approach will 
do the job?


Basically have a queue for point coordinates. Add your starting 
point to it (or multiple starting points). In a loop keep 
extracting points from the front of the queue, paint this point 
with a color and add non-painted neighbors of this point to the 
end of the queue. Keep going until the queue is empty.


Example:
```D
import std;

const width = 78;
const height = 10;
const number_of_starting_points = 5;

struct point { int x, y; }

void show_grid(char[][] grid) {
  foreach (ref row ; grid)
writeln(row);
  writeln;
}

void animated_fill(char[][] grid, point[] starting_points) {
  auto height = grid.length;
  if (height == 0)
return;
  auto width = grid[0].length;

  struct xpoint { int x, y, dist_from_start; }

  DList!xpoint queue;
  foreach (p ; starting_points)
queue.insertBack(xpoint(p.x, p.y, 0));

  int current_dist = 0;
  while (!queue.empty) {
  auto p = queue.front;
  queue.removeFront;

  if (grid[p.y][p.x] != '.')
continue; // only fill the dots

  if (p.dist_from_start > current_dist) {
show_grid(grid);
current_dist = p.dist_from_start;
  }

  grid[p.y][p.x] = '#';

  if (p.y + 1 < height)
queue.insertBack(xpoint(p.x, p.y + 1, p.dist_from_start + 
1));

  if (p.y - 1 >= 0)
queue.insertBack(xpoint(p.x, p.y - 1, p.dist_from_start + 
1));

  if (p.x + 1 < width)
queue.insertBack(xpoint(p.x + 1, p.y, p.dist_from_start + 
1));

  if (p.x - 1 >= 0)
queue.insertBack(xpoint(p.x - 1, p.y, p.dist_from_start + 
1));

  }
  show_grid(grid);
}

void main() {
  auto grid = new char[][](height, width);
  foreach (ref row ; grid)
row[] = '.';

  auto random_points = new point[](number_of_starting_points);
  foreach (ref p ; random_points)
p = point(uniform(0, width), uniform(0, height));

  animated_fill(grid, random_points);
}
```

Instead of a slow DList, it's also possible to just use a regular 
static array and two indexes for the start and the end of the 
queue. With a good implementation of BFS, this array won't need 
to store more than `grid_width * grid_height` elements. My 
implementation isn't a good one, but can be improved to do it.


Re: Graphical progressive fill

2022-12-11 Thread thebluepandabear via Digitalmars-d-learn

On Sunday, 11 December 2022 at 06:50:44 UTC, Joel wrote:
I've been trying to fill in areas with a colour but can't work 
it out. I want something like the effect where it fills with 
diamonds. Not all at once but building up in the main program 
loop.



#  #
#  #
#  #  #
#  #
#  #



#  #
#  #  #
#   ####
#  #  #
#  #



## #
# ####
#  # #
# ###   #
## #



There should be some ANSI sequences that allow you to do such a 
thing (as in clearing the console and refreshing), not familiar 
with any though from the top of my head.


Re: Why can't rvalues be passed to a 'ref' parameter?

2022-12-11 Thread zjh via Digitalmars-d-learn

On Sunday, 11 December 2022 at 16:48:57 UTC, Ali Çehreli wrote:

However, the situation has changed in D: It has been possible 
to pass rvalues by reference through the magic of 'in' 
parameters. This currently requires the -preview=in compiler 
switch, which makes 'in' parameters imply 'const scope':


  https://dlang.org/spec/function.html#in-params

[...]


Thank you for your detailed `explanation`.




Re: arsd.jni

2022-12-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 12 December 2022 at 01:19:23 UTC, jni wrote:
The boilerplate is easy but Then the other part is a 
problem for me is the necessary other Java classes. They are 
not part of the NDK so the only way to load the jar is to use 
jni? Is that correct?


I haven't updated this for a while,but I translated the whole api 
back in 2020 for my d_android package


https://code.dlang.org/packages/d_android

see for example:
https://github.com/adamdruppe/d_android/blob/master/java_bindings/android/java/android/net/ConnectivityManager_d_interface.d


(but be warned if you import these it can be very slow to 
compile, so just bringing only the classes you need can be much 
faster)


I also translated the ndk headers to D back then:

https://github.com/adamdruppe/d_android/tree/master/translated_headers/android/ndk


But I never used android beyond hello world so i just don't know 
THAT much about it.


Re: arsd.jni

2022-12-11 Thread jni via Digitalmars-d-learn

On Sunday, 11 December 2022 at 14:24:18 UTC, Adam D Ruppe wrote:
I don't know how to do much of anything on Android, but if you 
can post a small Java code example, I can suggest how to use it 
from D. You can bind many classes through the jni.


The biggest limitation is you can't do callbacks or subclasses 
through android jni, so it is kinda limited for interactive 
components.





Always prefer to use Java code instead of NDK library code.
I'm not saying to not use D, I'm saying that using NDK libraries
such opensl es asset manager other ones are really trickier to 
use.

If you're just starting to code for android ndk
prefer doing the work inside java and using D to call
the java method that you need. This is the main advice
I can give you. And never use native activity.
Too much work for nothing.




Just as I thought. I had too much of Java years ago and I am 
not going through that again and will have to use the 
documentation! I would rather use Javascript! I kid. I know the 
hate. I understand, but it's not so bad I think. I make apps in 
Javascript easily. No problem at all. This occasion? It is not 
possible.
On the android forums they suggested I use a generator on the 
files I need from the NDK, but I will not do that and I will do 
the classes one by one by hand as I use them. I can't use Java. 
Like an old girlfriend once I say enough is enough I will go and 
I don't look back. I do like it better than Kotlin from what I 
have seen, so that is why I choose to do it all in D.
I will look through the NDK for the classes I need then. The 
two things that were most important for a reply from you lot are 
exactly on using these header files of inside the NDK? of which I 
only have used extern(C) on the moments after learning the D 
language. Too many years ago. That and I don't mind writing the 
boilerplate code as you say maybe I could try binding some if I 
can figure out how without writting any Java. That is why I was 
trying to avoid to use the jni. I mostly worry about keeping 
things maintainable for when I decide to use the module again for 
some other android project that I can not do with Javascript.
The boilerplate is easy but Then the other part is a problem 
for me is the necessary other Java classes. They are not part of 
the NDK so the only way to load the jar is to use jni? Is that 
correct? I saw a jartoD function in arsd.jni but I wasn't sure 
how it fit into the lib or what it was used for "translator"? I 
wasn't sure. For the Apache client library and the other 2 jar 
classes I will use with it then writting all the Java binding 
seems inevitable. This makes me want to use D more and more. A 
much better language in my opinion. If I ever decide that I 
should to use a C library as a replacement for the D curl library 
then that will be additional work. I was wanting to ask the 
experts to see the best way to do these but I guess there is no 
easy way. On github there was what I saw a partial NDK headers 
for D project but the classes I need were not there. I prefer NDK 
livrary code because it is more acceptable for me to read and 
understand the functional programming of C. All the classes of 
the framework API in the SDK give me a headache! The NDK is much 
nicer to menin that way.
What I want to do is fetch a file from the internet! That's 
it! How did I get in this mess?


Re: Function template as template parameter

2022-12-11 Thread Andrey Zherikov via Digitalmars-d-learn

On Sunday, 11 December 2022 at 16:24:30 UTC, Ali Çehreli wrote:

On 12/11/22 05:54, Salih Dincer wrote:
> On Sunday, 11 December 2022 at 09:43:34 UTC, Andrey Zherikov
wrote:
>> Note that callback parameter must be compile-time parameter
as it
>> represents a member of a type during introspection done by
`foo`.
>
> I can't quite understand the question, this already works:

I think the OP is trying to create an anonymous template.


Yes

The following does not work when one attempts to use the 
template anonymously. Just trying to pass a template just like 
a lambda


Thanks for confirming this.


Re: Why can't rvalues be passed to a 'ref' parameter?

2022-12-11 Thread ryuukk_ via Digitalmars-d-learn
On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear 
wrote:

Hello,

I am not really understanding why rvalues cannot be passed to a 
'ref' parameter, the explanation in the book about D I am 
reading was not clear:


"The main reason for this limitation is the fact that a 
function taking a ref
parameter can hold on to that reference for later use, at a 
time when the rvalue

would not be available."

I didn't really understand what Ali meant by this statement, 
any help would be appreciated so this can be clarified.


Regards,
thebluepandabear



You can if you compile with: ``-preview=rvaluerefparam``

I personally think this preview should be enabled by default

I use it all the time


```D
struct vec2{ float x; float y;}

void pass(const ref vec2 pos)
{}

void main()
{
pass( vec2(1,1) );
}
```


If you use dub:

```json
   "dflags": [
"-preview=rvaluerefparam",
],
```



Re: Is there such concept of a list in D?

2022-12-11 Thread ryuukk_ via Digitalmars-d-learn
On Saturday, 10 December 2022 at 05:46:26 UTC, thebluepandabear 
wrote:
In most languages there is some sort of `List` type, is that 
the same for D?


There is: https://dlang.org/phobos/std_container_dlist.html

Why is it called ``DList`` and not just ``List``, i have no clue


Re: Why can't rvalues be passed to a 'ref' parameter?

2022-12-11 Thread Ali Çehreli via Digitalmars-d-learn

On 12/11/22 01:25, zjh wrote:
> On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear wrote:
>
>> "The main reason for this limitation is the fact that a function
>> taking a ref
>> parameter can hold on to that reference for later use, at a time when
>> the rvalue
>> would not be available."
>>
>
>
> I only know that `rvalue` is a temporary value that will not be used,

Temporary values can be used but they should not be stored for later. In 
this context, if the ref parameter were also 'scope', we should be able 
to pass rvalues. (See below.)


> while `ref` is omitting a pointer.

Yes, although it is still a pointer behind the scenes, 'ref' is syntax 
that avoids pointers.


However, the situation has changed in D: It has been possible to pass 
rvalues by reference through the magic of 'in' parameters. This 
currently requires the -preview=in compiler switch, which makes 'in' 
parameters imply 'const scope':


  https://dlang.org/spec/function.html#in-params

import std.stdio;

struct S {
string id;
}

void foo(in S s) {
writeln("foo received ", s.id);
}

void main() {
auto s = S("lvalue");
foo(s);
foo(S("rvalue"));
}

Prints

foo received lvalue
foo received rvalue

On can add a copy constructor to S to see whether 'in' is by-copy or 
by-value.


Ali



Re: Function template as template parameter

2022-12-11 Thread Ali Çehreli via Digitalmars-d-learn

On 12/11/22 05:54, Salih Dincer wrote:
> On Sunday, 11 December 2022 at 09:43:34 UTC, Andrey Zherikov wrote:
>> Note that callback parameter must be compile-time parameter as it
>> represents a member of a type during introspection done by `foo`.
>
> I can't quite understand the question, this already works:

I think the OP is trying to create an anonymous template. I don't think 
it's possible.


The following works because the template has the name 'print':

// Named template:
static void print(int i)() {
import std;
writeln(i);
}

void main() {
foo!print;
}

The following does not work when one attempts to use the template 
anonymously. Just trying to pass a template just like a lambda:


foo!((int i)() {
import std;
writeln(i);
});

Note how '(int i)()' is hoping to define a template.

Ali



Re: arsd.jni

2022-12-11 Thread Adam D Ruppe via Digitalmars-d-learn
I don't know how to do much of anything on Android, but if you 
can post a small Java code example, I can suggest how to use it 
from D. You can bind many classes through the jni.


The biggest limitation is you can't do callbacks or subclasses 
through android jni, so it is kinda limited for interactive 
components.


Re: Function template as template parameter

2022-12-11 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 11 December 2022 at 09:43:34 UTC, Andrey Zherikov 
wrote:
Note that callback parameter must be compile-time parameter as 
it represents a member of a type during introspection done by 
`foo`.


I can't quite understand the question, this already works:

```d
void foo(alias callback)()
{
import std.stdio : writefln;
callback.writefln!"%s";
}

void main()
{
  auto i = [ 1, 2, 3 ];
  auto s = ["foo", "bar", "zoo"];

  foo!i;
  foo!s;

  foo!"hello, world!";

  enum a = 11, b = 22;
  foo!(a + b);
} /*
  [1, 2, 3]
  ["foo", "bar", "zoo"]
  hello, world!
  33
*/
```


Function template as template parameter

2022-12-11 Thread Andrey Zherikov via Digitalmars-d-learn

I have this (very simplified) code:
```d
void foo(alias callback)()
{
callback!5;
}

static void print(int i)() { writeln(i); }
foo!print;
```

Is there a way to merge two last lines into one of a form like 
`foo!(...something...)`?


Note that callback parameter must be compile-time parameter as it 
represents a member of a type during introspection done by `foo`.


Re: Why can't rvalues be passed to a 'ref' parameter?

2022-12-11 Thread zjh via Digitalmars-d-learn
On Sunday, 11 December 2022 at 04:36:45 UTC, thebluepandabear 
wrote:


"The main reason for this limitation is the fact that a 
function taking a ref
parameter can hold on to that reference for later use, at a 
time when the rvalue

would not be available."




I only know that `rvalue` is a temporary value that will not be 
used, while `ref` is omitting a pointer.




Re: Is there such concept of a list in D?

2022-12-11 Thread zjh via Digitalmars-d-learn

On Sunday, 11 December 2022 at 07:47:35 UTC, Salih Dincer wrote:


..


Thank you for your reply. I think if you take `random` values 
frequently, you'd better use `'array'`,am I right?