Separate IP parts

2016-12-09 Thread brocolis via Digitalmars-d-learn

How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190


std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

I wonder what's the equivalent D code.




Re: polar coordinates with ggplotd

2016-09-19 Thread brocolis via Digitalmars-d-learn
On Monday, 19 September 2016 at 12:34:56 UTC, Edwin van Leeuwen 
wrote:

On Sunday, 18 September 2016 at 22:13:35 UTC, brocolis wrote:

Found an error in ys line. Thanks.


Does that mean you solved it?

Currently there is no special support for other coordinate 
systems, but I recently added Guides for x/y coordinates which 
should make this relatively straightforward to implement and is 
next on the list. Not sure when I'll get a chunk of time to 
implement it though.


For now you will have to convert the coordinates yourself, 
before plotting them.


Yeah, problem solved. Thanks.



Re: polar coordinates with ggplotd

2016-09-18 Thread brocolis via Digitalmars-d-learn

On Sunday, 18 September 2016 at 22:07:31 UTC, brocolis wrote:

I've tried this code.

import ggplotd.ggplotd;
import ggplotd.geom;
import ggplotd.aes;
import ggplotd.axes;
import std.math;

auto r(double theta)
{
return 2 * sin(6*theta);
}

auto getX(double theta)
{
return r(theta) * cos(theta);
}

auto getY(double theta)
{
return r(theta) * sin(theta);
}

void main()
{
import std.array : array;
import std.algorithm : map;
import std.range : iota;

auto theta = iota(0, 2*PI, 0.1).array;
auto xs = theta.map!((x) => getX(x)).array;
auto ys = xs.map!((x) => getY(x)).array;

auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ys), "y")(xs, ys) ) );


gg.put( xaxisRange(-5, 5) ).put( xaxisLabel( "x" ) );
gg.put( yaxisRange(-5, 5) ).put( yaxisLabel( "y" ) );
gg.put( xaxisOffset(0) ).put( yaxisOffset(0) );
gg.save( "output.png", 500, 300 );
}

And I got the output: http://imgur.com/KwLYJpN

Expected:
https://www.wolframalpha.com/input/?i=polar+plot+2*sin(6*theta)

I need to somehow activate "polar" mode in ggplotd. Thanks.


Found an error in ys line. Thanks.



polar coordinates with ggplotd

2016-09-18 Thread brocolis via Digitalmars-d-learn

I've tried this code.

import ggplotd.ggplotd;
import ggplotd.geom;
import ggplotd.aes;
import ggplotd.axes;
import std.math;

auto r(double theta)
{
return 2 * sin(6*theta);
}

auto getX(double theta)
{
return r(theta) * cos(theta);
}

auto getY(double theta)
{
return r(theta) * sin(theta);
}

void main()
{
import std.array : array;
import std.algorithm : map;
import std.range : iota;

auto theta = iota(0, 2*PI, 0.1).array;
auto xs = theta.map!((x) => getX(x)).array;
auto ys = xs.map!((x) => getY(x)).array;

auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ys), "y")(xs, ys) ) );


gg.put( xaxisRange(-5, 5) ).put( xaxisLabel( "x" ) );
gg.put( yaxisRange(-5, 5) ).put( yaxisLabel( "y" ) );
gg.put( xaxisOffset(0) ).put( yaxisOffset(0) );
gg.save( "output.png", 500, 300 );
}

And I got the output: http://imgur.com/KwLYJpN

Expected:
https://www.wolframalpha.com/input/?i=polar+plot+2*sin(6*theta)

I need to somehow activate "polar" mode in ggplotd. Thanks.





Re: Draw math formulas with ggplotd

2016-09-18 Thread brocolis via Digitalmars-d-learn
On Saturday, 17 September 2016 at 11:45:07 UTC, Edwin van Leeuwen 
wrote:
On Saturday, 17 September 2016 at 11:22:04 UTC, John Colvin 
wrote:

On Saturday, 17 September 2016 at 02:41:15 UTC, brocolis wrote:
How do I draw math formulas programmatically? I want to do on 
screen what latex does on .pdf.


And I want to draw a math formula in the image generated with 
ggplotd.


Generate data from those formulas (I like to do this with 
something like iota(0, 10, 0.05).map!(x => sqrt(x) / (1 + 
sin(x)^^2)) and then plot that.


For this part ggplotd does have a helper function:

http://blackedder.github.io/ggplotd/ggplotd/stat.html#statFunction

auto gg = statFunction(x => sqrt(x) / (1 +
  sin(x)^^2), 0.0, 10).geomLine().putIn(GGPlotD());

But I assumed he meant adding the formula onto the plot.


Yes I want adding the formula onto the plot. Thanks.


Draw math formulas with ggplotd

2016-09-16 Thread brocolis via Digitalmars-d-learn
How do I draw math formulas programmatically? I want to do on 
screen what latex does on .pdf.


And I want to draw a math formula in the image generated with 
ggplotd.




Re: ggplotd - curve colour

2016-05-09 Thread brocolis via Digitalmars-d-learn

On Monday, 9 May 2016 at 06:24:22 UTC, Edwin van Leeuwen wrote:

On Monday, 9 May 2016 at 02:29:47 UTC, brocolis wrote:

Is this correct usage?
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );


The output is a blank png file.

Full source:
import ggplotd.ggplotd;
import ggplotd.geom;
import ggplotd.aes;
import ggplotd.axes;

void main()
{
import std.array : array;
import std.algorithm : map;
import std.range : iota;
import ggplotd.colour;

auto f = (double x) { return x; };
auto xs = iota(-5, 5, 0.1 ).array;
auto ysfit = xs.map!((x) => f(x)).array;
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );


gg.put( xaxisOffset( 0) ).put( yaxisOffset( 0) );
gg.save( "axes.png", 500, 300 );
}


The problem there is that colour also needs to be an 
InputRange. This is so that different points can have a 
different colours associated with it, which is particularly 
useful if you want to plot some data and have different types 
of data plotted as different colours.


In your example you can either do:

```
auto colour = "red".repeat( xs.length );
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x",
 typeof(ysfit), "y", typeof(colour), "colour")( xs, ysfit, 
colour) ) );

```

Or use the mergeRange function mentioned before, which will 
automatically repeat if one argument is a single element.


```
auto aes = Tuple!( string, "colour" )( "red" ).mergeRange( 
Aes!(typeof(xs), "x",

 typeof(ysfit), "y" )( xs, ysfit ) );
```


OK! Thank you.



Re: ggplotd - curve colour

2016-05-08 Thread brocolis via Digitalmars-d-learn

Is this correct usage?
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );


The output is a blank png file.

Full source:
import ggplotd.ggplotd;
import ggplotd.geom;
import ggplotd.aes;
import ggplotd.axes;

void main()
{
import std.array : array;
import std.algorithm : map;
import std.range : iota;
import ggplotd.colour;

auto f = (double x) { return x; };
auto xs = iota(-5, 5, 0.1 ).array;
auto ysfit = xs.map!((x) => f(x)).array;
auto gg = GGPlotD().put( geomLine( Aes!(typeof(xs), "x", 
typeof(ysfit), "y", string, "colour")( xs, ysfit, "red") ) );


gg.put( xaxisOffset( 0) ).put( yaxisOffset( 0) );
gg.save( "axes.png", 500, 300 );
}




ggplotd - curve colour

2016-05-07 Thread brocolis via Digitalmars-d-learn

How do I set the color of a curve with ggplotd?
Thanks.