From: Aditya Mahajan <adityam@umich.edu>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: How can I compare picture variables in metapost?
Date: Tue, 5 May 2009 23:09:43 -0400 (EDT) [thread overview]
Message-ID: <alpine.LNX.2.00.0905052306550.6547@ybpnyubfg.ybpnyqbznva> (raw)
In-Reply-To: <alpine.LNX.2.00.0905052205570.6547@ybpnyubfg.ybpnyqbznva>
On Tue, 5 May 2009, Aditya Mahajan wrote:
> Both Taco's and my solutions can be adapted so that you do not randomize the
> radius. (Taco's solution will also work for arbitrary object that can then be
> rotated by a random amount).
Here is another idea. Ask metapost to test for intersection, but code the
rest of the logic in lua. Below is a proof of concept code for
communicating with metapost.
\startluacode
circle = circle or {}
function circle.path(x,y,r, name)
local path = "path " .. name .. "; \n" ..
name .. "= fullcircle scaled " .. 2*r ..
" shifted (" .. x .. "," .. y ..") ; \n"
return path
end
function circle.intersect(x1,y1,r1,x2,y2,r2)
local mpx = metapost.format("metafun")
local c = circle.path(x1,y1,r1, "c")
local d = circle.path(x2,y2,r2, "d")
local intersect = "pair n; n := c intersectiontimes d ; \n"
local message = "if (xpart n) < 0 : \n" ..
" message(\"**false**\") ; \n" ..
"else : \n" ..
" message(\"**true**\") ; \n" ..
" endif \n"
local file = c .. d .. intersect .. message
-- print(file)
local result = mpx:execute(file)
local log = result.log
if result.status < 2 then
print("Metapost run successful *****************")
circle.show_result(x1,y1,r1,x2,y2,r2,circle.parse(log))
else
print("Metapost run unsuccessful *****************")
end
print(log)
end
function circle.parse(log)
local space = lpeg.S(" \t\n")
local yes = lpeg.C("**true**")
local no = lpeg.C("**false**")
local result = (yes + no) / circle.result
local parser = space^0 * result * space^0
return parser:match(log)
end
function circle.result(str)
return str == "**true**"
end
function circle.show_result(x1,y1,r1,x2,y2,r2,flag)
local tprint = function(s) tex.sprint(tex.ctxcatcodes,s) end
local result = "Circles (" .. x1 .. "," .. y1 .. "):" .. r1 ..
" and (" .. x2 .. "," .. y2 .. "):" .. r2 .. " "
if flag then
result = result .. " intersect. "
else
result = result .. " do not intersect. "
end
tprint (result)
end
\stopluacode
\starttext
\startluacode
circle.intersect(1,1,1,2,2,1.5)
circle.intersect(1,1,1,3,3,0.5)
\stopluacode
\stoptext
___________________________________________________________________________________
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 : https://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
next prev parent reply other threads:[~2009-05-06 3:09 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-05 12:27 Zhichu Chen
2009-05-05 12:51 ` Taco Hoekwater
2009-05-05 13:51 ` Zhichu Chen
2009-05-05 15:04 ` Taco Hoekwater
2009-05-05 15:19 ` Zhichu Chen
2009-05-05 16:06 ` Taco Hoekwater
2009-05-05 16:54 ` Taco Hoekwater
2009-05-05 21:42 ` Peter Rolf
2009-05-05 23:28 ` Aditya Mahajan
2009-05-06 1:46 ` Zhichu Chen
2009-05-06 2:10 ` Aditya Mahajan
2009-05-06 3:09 ` Aditya Mahajan [this message]
2009-05-06 9:06 ` Peter Rolf
2009-05-05 22:57 ` Aditya Mahajan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LNX.2.00.0905052306550.6547@ybpnyubfg.ybpnyqbznva \
--to=adityam@umich.edu \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).