ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* plot data from file: cld
@ 2015-01-03 23:43 John Kitzmiller
  0 siblings, 0 replies; 5+ messages in thread
From: John Kitzmiller @ 2015-01-03 23:43 UTC (permalink / raw)
  To: ntg-context

I seek how to use context-lua to draw a path from ‘points’ in an external file. 

Below is one way to plot points from an external file, call the file DATA with contents:

1 3
-2 2
-3 -1
3 -2

\starttext
\startluacode
local metafun = context.metafun
metafun.start()

io.input(“DATA")
while true do
  local x, y = io.read("*n", "*n")
  if not x then break end
  metafun("filldraw fullcircle scaled 2mm shifted(%dcm,%dcm);",x,y)
end

metafun.stop()
\stopluacode
\stoptext


How to make this a polygon?

I know other ways to read and manipulate DATA with Lua—it is the drawing part where I most look for help.

Thanks,
John


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: plot data from file: cld
       [not found] <mailman.88.1420455074.2429.ntg-context@ntg.nl>
@ 2015-01-05 22:07 ` John Kitzmiller
  0 siblings, 0 replies; 5+ messages in thread
From: John Kitzmiller @ 2015-01-05 22:07 UTC (permalink / raw)
  To: ntg-context; +Cc: Hans Hagen


[-- Attachment #1.1: Type: text/plain, Size: 1827 bytes --]

On Jan 5, 2015, at 5:51 AM, Hans Hagen <pragmz@wxs.nl <mailto:pragmz@wxs.nl>> wrote:

>> On 1/4/2015 6:04 AM, Akira Kakuto wrote:
>> Dear John,
>> 
>>> How to make this a polygon?
>> 
>> An example:
>> 
>> \starttext
>> \startluacode
>> local metafun = context.metafun
>> metafun.start()
>> io.input("DATA")
>> local i, j
>> local x, y, s, t1, t2
>> i = 1
>> x={}
>> y={}
>> s="draw "
>> while true do
>>  x[i], y[i] = io.read("*n", "*n")
>>  if not x[i] then break end
>>  metafun("filldraw fullcircle scaled 2mm shifted(%dcm,%dcm);",x[i],y[i])
>>  i = i + 1
>> end
>> i = i - 1
>> for j = 1, i do
>>  t1 = tostring(x[j])
>>  t2 = tostring(y[j])
>>  s = s.."("..t1.."cm,"..t2.."cm)--"
>> end
>> t1 = tostring(x[1])
>> t2 = tostring(y[1])
>> s = s.."("..t1.."cm,"..t2.."cm)"
>> s = s.." withpen pencircle scaled 1bp;"
>> metafun(s)
>> metafun.stop()
>> \stopluacode
>> \stoptext
> 
> Here is a bit more compact version. A path can normally nicely be concat 
> using "--" or ".." and we have a metafun helper drawpoints.
> 
> \starttext
> \startluacode
> context.metafun.start()
> local split = utilities.parsers.csvsplitter { separator = " " }
> local data = split(io.loaddata("data.txt"))
> for i=1,#data do
>     data[i] = string.formatters["(%s,%s)"](unpack(data[i]))
> end
> context.metafun("path p ; p := (%s -- cycle) scaled cm 
> ;",table.concat(data,"--"))
> context.metafun("draw       p withpen pencircle scaled 0.25mm ;")
> context.metafun("drawpoints p withpen pencircle scaled 2.00mm ;")
> context.metafun.stop()
> \stopluacode
> \stoptext

Thank you Hans. A few tools new to me in there!

I am wondering (and will try to find) if multiple paths could be contained in the external file— a marker and test structure?

Thanks again,

John





[-- Attachment #1.2: Type: text/html, Size: 3120 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: plot data from file: cld
       [not found] <mailman.1.1420369201.5393.ntg-context@ntg.nl>
@ 2015-01-04 16:43 ` John Kitzmiller
  0 siblings, 0 replies; 5+ messages in thread
From: John Kitzmiller @ 2015-01-04 16:43 UTC (permalink / raw)
  To: ntg-context; +Cc: Akira Kakuto


[-- Attachment #1.1: Type: text/plain, Size: 574 bytes --]

On Jan 4, 2015, at 6:00 AM, ntg-context-request@ntg.nl wrote:
> Dear John,
> 
>> How to make this a polygon?
> 
> An example:

Many thanks to you Akira.

Reading the file into lua tables is good sense, and thank you for the string manipulations to pass to metafun().

From your teaching I changed this assignment:

> t1 = tostring(x[1])
> t2 = tostring(y[1])
> s = s.."("..t1.."cm,"..t2.."cm)"
> s = s.." withpen pencircle scaled 1bp;"

to: s = s.."cycle withpen pencircle scaled 1bp;”

to close the polygon.

With respect,

John






 


[-- Attachment #1.2: Type: text/html, Size: 1858 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: plot data from file: cld
  2015-01-04  5:04 Akira Kakuto
@ 2015-01-04 12:03 ` Hans Hagen
  0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2015-01-04 12:03 UTC (permalink / raw)
  To: ntg-context

On 1/4/2015 6:04 AM, Akira Kakuto wrote:
> Dear John,
>
>> How to make this a polygon?
>
> An example:
>
> \starttext
> \startluacode
> local metafun = context.metafun
> metafun.start()
> io.input("DATA")
> local i, j
> local x, y, s, t1, t2
> i = 1
> x={}
> y={}
> s="draw "
> while true do
>   x[i], y[i] = io.read("*n", "*n")
>   if not x[i] then break end
>   metafun("filldraw fullcircle scaled 2mm shifted(%dcm,%dcm);",x[i],y[i])
>   i = i + 1
> end
> i = i - 1
> for j = 1, i do
>   t1 = tostring(x[j])
>   t2 = tostring(y[j])
>   s = s.."("..t1.."cm,"..t2.."cm)--"
> end
> t1 = tostring(x[1])
> t2 = tostring(y[1])
> s = s.."("..t1.."cm,"..t2.."cm)"
> s = s.." withpen pencircle scaled 1bp;"
> metafun(s)
> metafun.stop()
> \stopluacode
> \stoptext

Here is a bit more compact version. A path can normally nicely be concat 
using "--" or ".." and we have a metafun helper drawpoints.

\starttext
\startluacode
context.metafun.start()
local split = utilities.parsers.csvsplitter { separator = " " }
local data = split(io.loaddata("data.txt"))
for i=1,#data do
     data[i] = string.formatters["(%s,%s)"](unpack(data[i]))
end
context.metafun("path p ; p := (%s -- cycle) scaled cm 
;",table.concat(data,"--"))
context.metafun("draw       p withpen pencircle scaled 0.25mm ;")
context.metafun("drawpoints p withpen pencircle scaled 2.00mm ;")
context.metafun.stop()
\stopluacode
\stoptext




-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* plot data from file: cld
@ 2015-01-04  5:04 Akira Kakuto
  2015-01-04 12:03 ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Akira Kakuto @ 2015-01-04  5:04 UTC (permalink / raw)
  To: ntg-context

Dear John,

> How to make this a polygon?

An example:

\starttext
\startluacode
local metafun = context.metafun
metafun.start()
io.input("DATA")
local i, j
local x, y, s, t1, t2
i = 1
x={}
y={}
s="draw "
while true do
  x[i], y[i] = io.read("*n", "*n")
  if not x[i] then break end
  metafun("filldraw fullcircle scaled 2mm shifted(%dcm,%dcm);",x[i],y[i])
  i = i + 1
end
i = i - 1
for j = 1, i do
  t1 = tostring(x[j])
  t2 = tostring(y[j])
  s = s.."("..t1.."cm,"..t2.."cm)--"
end
t1 = tostring(x[1])
t2 = tostring(y[1])
s = s.."("..t1.."cm,"..t2.."cm)"
s = s.." withpen pencircle scaled 1bp;"
metafun(s)
metafun.stop()
\stopluacode
\stoptext

Best,
Akira

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-01-05 22:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-03 23:43 plot data from file: cld John Kitzmiller
2015-01-04  5:04 Akira Kakuto
2015-01-04 12:03 ` Hans Hagen
     [not found] <mailman.1.1420369201.5393.ntg-context@ntg.nl>
2015-01-04 16:43 ` John Kitzmiller
     [not found] <mailman.88.1420455074.2429.ntg-context@ntg.nl>
2015-01-05 22:07 ` John Kitzmiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).