[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

MetaPost export?



Posted for Johannes Hsing <hannes@ruhrau.de>.

---------- Forwarded message ----------
Date: Wed,  5 Mar 2003 16:51:45 -0500 (EST)
From: owner-graphthing-discuss@seul.org
To: graphthing-discuss-approval@seul.org
Subject: BOUNCE graphthing-discuss@seul.org:    Non-member submission from
    [[iso-8859-1] Johannes Hüsing <hannes@ruhrau.de>]   

>From owner-graphthing-discuss@seul.org  Wed Mar  5 16:51:45 2003
Return-Path: <owner-graphthing-discuss@seul.org>
Delivered-To: graphthing-discuss@seul.org
Received: from mail1.panix.com (mail1.panix.com [166.84.1.72])
	by moria.seul.org (Postfix) with ESMTP id 528A533FC6
	for <graphthing-discuss@seul.org>; Wed,  5 Mar 2003 16:51:45 -0500 (EST)
Received: from qossuth (dfn-home230.extern.uni-essen.de [132.252.244.230])
	by mail1.panix.com (Postfix) with ESMTP id 7C10248E19
	for <graphthing-discuss@seul.org>; Wed,  5 Mar 2003 16:51:44 -0500 (EST)
Received: from hannes by qossuth with local (Exim 3.35 #1 (Debian))
	id 18qh2e-0000vV-00
	for <graphthing-discuss@seul.org>; Wed, 05 Mar 2003 23:07:12 +0100
Date: Wed, 5 Mar 2003 23:07:11 +0100
To: graphthing-discuss@seul.org
Subject: MetaPost export?
Message-ID: <20030305220711.GC3430@ruhrau.de>
Reply-To: johannes.huesing@ruhrau.de
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
User-Agent: Mutt/1.3.28i
From: =?iso-8859-1?Q?Johannes_H=FCsing?= <hannes@ruhrau.de>

Greetings,

I like this tool and I would like to see it develop!

one thing that comes to my mind immediately when I see graphs is:
Why doesn't this program support MetaPost?

The following routine, when placed into graph.cc, should do
an export in MetaPost syntax. Maybe somebody out there could
consider developing this export filter.

The nice things about MetaPost are: Vertices are variables and can
be readdressed. Edges can be too (not in the attached export filter).
Arrows are a breeze.

WARNING: I know next to nothing about C++, so the attached code is 
likely to crash the program, pose a security hole, or worse.
Yes, I stole the print routine code.

Cheers


Johannes

--8-<--
void Graph::toMP (const char *fname, bool labels, bool weights) const
{
	std::fstream fs;
	int minx, miny, maxx, maxy;
	time_t timep;
	Graph::e_const_iterator eit;
	Graph::v_const_iterator vit;

	fs.open (fname, std::fstream::out);
	if (!fs.is_open ()) {
		/* std::cerr << "*** Couldn't open \"" << fname << "\"\n"; */
		throw std::runtime_error ("Couldn't open file.");
	}

	minx = miny = 999999;
	maxx = maxy = -1;
	for (vit = v_begin (); vit != v_end (); ++vit) {
		if (minx > (*vit)->x)
			minx = (*vit)->x;
		if (maxx < (*vit)->x)
			maxx = (*vit)->x;
		if (miny > (*vit)->y)
			miny = (*vit)->y;
		if (maxy < (*vit)->y)
			maxy = (*vit)->y;
	}
	if (V.size () < 1) {
		minx = miny = 10;
		maxx = maxy = 200;
	}
	minx -= 10;
	miny -= (labels ? 10 : 20);
	maxx += (labels ? 20 : 10);
	maxy += 20;

	timep = time (0);

	fs <<	"% Title: " << fname << "\n"
		"% created by: GraphThing " GT_VERSION
			  "   (c) 2001-2003 David Symonds\n"
		"% on:        " << ctime (&timep) << "\n"
	  /*  Vertices are defined by z[]  */
	  for (vit = v_begin (); vit != v_end (); ++vit)
	    fs << 'z[' << vit-v_begin () << ']=(' 
	       << (*vit)->x << ', ' << (*vit)->y << ')';

	fs << "\nbeginfig(1)\n";

	/* We dump the edges first so that the vertices will be printed
	 * "on top" of the edge ends
	 */

	/* Dump edges first */
	/* TODO: Handle directed edges, and edge weights */
	/* directed edges are done easily in MetaPost */
	/* by drawarrow instead of draw               */
	fs << "% Edges\n";
	for (eit = e_begin (); eit != e_end (); ++eit)
		fs << '  draw z[' (*eit)->v-v_begin () << ']--[' << (*eit)->v-v_begin () << "]\n";
	fs << '\n';

	/* Now dump the vertices */
	fs << "% Vertices\n";
	for (vit = v_begin (); vit != v_end (); ++vit)
	  fs << '  draw z[' << vit-v_begin () << "\n";
	if (labels) {
	  for (vit = v_begin (); vit != v_end (); ++vit)
	    fs << '  label.lft(' << (*vit)->v_label << ', z[', vit-v_begin (),"])\n";
	}
	fs << "\nendfig\nend;\n";

	fs.close ();
}

-->-8--


-- 
Johannes Hüsing   There is something fascinating about science. One gets
hannes@ruhrau.de  such wholesale returns of conjecture from such a 
                  trifling investment of fact.                Mark Twain