From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/50041 Path: news.gmane.org!not-for-mail From: Aditya Mahajan Newsgroups: gmane.comp.tex.context Subject: Re: How can I compare picture variables in metapost? Date: Tue, 5 May 2009 18:57:31 -0400 (EDT) Message-ID: References: <769ba7780905050527o4f42f9acod279bcce641189c@mail.gmail.com> <4A00366A.1040907@elvenkind.com> <769ba7780905050651l6c8601aer421b04c1b7e1c20c@mail.gmail.com> <4A00556C.5000503@elvenkind.com> <769ba7780905050819o68b79a5ek4741bfbb6fded7b6@mail.gmail.com> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; Format="flowed" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1241564323 16237 80.91.229.12 (5 May 2009 22:58:43 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 May 2009 22:58:43 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Wed May 06 00:58:33 2009 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by lo.gmane.org with esmtp (Exim 4.50) id 1M1Tan-0004Sw-Se for gctc-ntg-context-518@m.gmane.org; Wed, 06 May 2009 00:58:29 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 410511FC5A; Wed, 6 May 2009 00:58:28 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 26031-05; Wed, 6 May 2009 00:58:07 +0200 (CEST) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 4645A1FC32; Wed, 6 May 2009 00:58:07 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id DB54D1FC32 for ; Wed, 6 May 2009 00:58:04 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 29360-04-5 for ; Wed, 6 May 2009 00:57:56 +0200 (CEST) Original-Received: from filter3-nij.mf.surf.net (filter3-nij.mf.surf.net [195.169.124.154]) by ronja.ntg.nl (Postfix) with ESMTP id 2D4AD1FC2F for ; Wed, 6 May 2009 00:57:56 +0200 (CEST) Original-Received: from hackers.mr.itd.umich.edu (smtp.mail.umich.edu [141.211.14.81]) by filter3-nij.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id n45Mvsww014075 for ; Wed, 6 May 2009 00:57:54 +0200 Original-Received: FROM [192.168.2.8] (c-76-127-178-8.hsd1.ct.comcast.net [76.127.178.8]) By hackers.mr.itd.umich.edu ID 4A00C470.BBE22.11253 ; Authuser adityam; 5 May 2009 18:57:52 EDT In-Reply-To: <769ba7780905050819o68b79a5ek4741bfbb6fded7b6@mail.gmail.com> User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) X-CanIt-Geo: ip=141.211.14.81; country=US; region=MI; city=Ann Arbor; postalcode=48109; latitude=42.2923; longitude=-83.7145; metrocode=505; areacode=734; http://maps.google.com/maps?q=42.2923,-83.7145&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 219823022 - bae0d5112fcd - 20090506 X-Scanned-By: CanIt (www . roaringpenguin . com) on 195.169.124.154 X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.11 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:50041 Archived-At: On Tue, 5 May 2009, Zhichu Chen wrote: > On Tue, May 5, 2009 at 11:04 PM, Taco Hoekwater wrote: >> Zhichu Chen wrote: >>> >>> What I want exactly is how to determine if there's anything on >>> some region of the picture. I need this to test if the random >>> point I picked is useful. >> >> That is easy to answer: you can't (well, not unless you invest a *lot* >> of effort into creating a bitmap edge structure). > Well, quick and pain. >> >> However what you can do is ask metapost to calculate intersectionpoints >> with (the most likely ones of) the already existing objects. This may >> be the easiest solution (even though it will be so slow that for large >> numbers of items you may be forced to start a division tree). >> >> The core trick is that you randomly place a circle with random radius >> inside an x-y field, and you keep those paths/pictures in an array. For >> each newlyt generated circle, you look for an intersection with all the >> already existing ones (and the rectangle borders) and keep trying >> to re-place it until there are no more collisions. > > Seems that I don't have too many choices. Maybe using lua to do the > math and throwing the result to metapost is faster? I think I can do > this, but I don't know how. The documents are a little limited. Here is an attempt with lua. Aditya \startluacode third = third or {} function third.generate (x,y,r) return { ["center"] = {math.random() * x, math.random()*y} , ["radius"] = math.random() * r } end function third.distance (p1, p2) return math.sqrt( (p1[1] - p2[1])^2 + (p1[2] - p2[2])^2 ) end function third.feasible (c, list) if list then for i,v in pairs(list) do if third.distance (c["center"], v["center"]) < c["radius"] + v["radius"] then return false end end end return true end function third.add_circle (x,y,r,list) local c = {} repeat c = third.generate(x,y,r) until third.feasible(c,list) return c end function third.fill_circles(n,x,y,r) local list = {} for i=1,n do table.insert(list, third.add_circle(x,y,r, list)) end return list end function third.toMP(c, scale) local tprint = function(s) tex.sprint(tex.ctxcatcodes,s) end local scaled = function(p) return ("(" .. p .. "*" .. scale .. ")") end tprint("draw fullcircle scaled " .. scaled(2*c["radius"]) .. " shifted (" .. scaled(c["center"][1]) .. "," .. scaled(c["center"][2]) .. "); \n") end function third.show_circles(n,x,y,r) local tprint = function(s) tex.sprint(tex.ctxcatcodes,s) end local list = third.fill_circles(n,x,y,r) tprint("\\startMPcode") for i,v in pairs(list) do third.toMP(v, "1cm") end tprint("\\stopMPcode") end \stopluacode \def\drawCircles{\ctxlua{third.show_circles(100,10,10,2)}} \starttext \drawCircles \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 ___________________________________________________________________________________