<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: graphical code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:02:10 GMT</pubDate>
    <description>DZone Snippets: graphical code</description>
    <item>
      <title>A solution for the "Graphical Editor" problem</title>
      <link>http://snippets.dzone.com/posts/show/5245</link>
      <description>A solution for the "Graphical Editor" problem.&lt;br /&gt; &lt;br /&gt;Problem description:&lt;br /&gt;&lt;a href="http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html"&gt;http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Author: &lt;a href="http://www.inf.ufrgs.br/~jmftrindade"&gt;Joana Matos Fonseca da Trindade&lt;/a&gt;&lt;br /&gt;Date: 2008.03.12&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* &lt;br /&gt; * Solution for the "Graphical Editor" problem.&lt;br /&gt; * UVa ID: 10267&lt;br /&gt; */&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAX 250&lt;br /&gt;#define OFFSET 1&lt;br /&gt;#define DOS_NAME 12&lt;br /&gt;&lt;br /&gt;/* global image bounds */&lt;br /&gt;int n, m;&lt;br /&gt;&lt;br /&gt;/* fills a rectangle with the specified color */&lt;br /&gt;int fillRectangle(int m_ini, int n_ini, int m_end, int n_end, char color, char pTable[][MAX+OFFSET]) {&lt;br /&gt;	int i, j;&lt;br /&gt;	for (i = n_ini; i &lt;= n_end; i++) {&lt;br /&gt;		for (j = m_ini; j &lt;= m_end; j++) {&lt;br /&gt;			pTable[i][j] = color;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* fills a region R with the specified color */&lt;br /&gt;int fillRegion(int x, int y, char oldColor, char newColor, char pTable[][MAX+OFFSET]) {	&lt;br /&gt;	/* (x,y) is in region R */&lt;br /&gt;	pTable[y][x] = newColor;&lt;br /&gt;	&lt;br /&gt;	/* recursively check all 4 directions for neighbours of (x,y) with same color */&lt;br /&gt;	if ((pTable[y][x-1] == oldColor) &amp;&amp; (x &gt; OFFSET)) {         &lt;br /&gt;		fillRegion(x-1, y, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y][x+1] == oldColor) &amp;&amp; (x &lt; m)) {       &lt;br /&gt;		fillRegion(x+1, y, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y-1][x] == oldColor) &amp;&amp; (y &gt; OFFSET)) {        &lt;br /&gt;		fillRegion(x, y-1, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y+1][x] == oldColor) &amp;&amp; (y &lt; n)) {        &lt;br /&gt;		fillRegion(x, y+1, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* outputs the image */&lt;br /&gt;int printImage(int m, int n, char pTable[][MAX+OFFSET]) {&lt;br /&gt;	int i, j;	&lt;br /&gt;	for (i = OFFSET; i &lt; n+OFFSET; i++) {&lt;br /&gt;		for (j = OFFSET; j &lt; m+OFFSET; j++ ) {&lt;br /&gt;			printf("%c", pTable[i][j]);&lt;br /&gt;		}&lt;br /&gt;		printf("\n");&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* main */&lt;br /&gt;int main (int argc, const char * argv[]) {&lt;br /&gt;	/* the image */&lt;br /&gt;	char image[MAX+OFFSET][MAX+OFFSET];&lt;br /&gt;&lt;br /&gt;	/* editor command */&lt;br /&gt;	char command;&lt;br /&gt;	&lt;br /&gt;	/* coords */&lt;br /&gt;	int x1, x2, y1, y2, tmp;&lt;br /&gt;	&lt;br /&gt;	/* colors */&lt;br /&gt;	char color, oldColor;&lt;br /&gt;	&lt;br /&gt;	/* filename */&lt;br /&gt;	char filename[DOS_NAME+1];&lt;br /&gt;			&lt;br /&gt;	while(scanf("%c", &amp;command) != EOF) {		&lt;br /&gt;		/* X, terminates the session */&lt;br /&gt;		if (command == 'X') {&lt;br /&gt;			return 0;&lt;br /&gt;		}		&lt;br /&gt;		switch (command) {&lt;br /&gt;			/* create image */&lt;br /&gt;			case 'I' :&lt;br /&gt;				scanf("%d %d", &amp;m, &amp;n);&lt;br /&gt;				fillRectangle(1, 1, m, n, 'O', image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* clear image */&lt;br /&gt;			case 'C' :&lt;br /&gt;				fillRectangle(1, 1, m, n, 'O', image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* colors a pixel */&lt;br /&gt;			case 'L' :&lt;br /&gt;				scanf("%d %d %c", &amp;x1, &amp;y1, &amp;color);&lt;br /&gt;				image[y1][x1] = color;&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw vertical segment */&lt;br /&gt;			case 'V' :&lt;br /&gt;				scanf("%d %d %d %c", &amp;x1, &amp;y1, &amp;y2, &amp;color);&lt;br /&gt;				if (y2 &gt;= y1)&lt;br /&gt;					fillRectangle(x1, y1, x1, y2, color, image);&lt;br /&gt;				else&lt;br /&gt;					fillRectangle(x1, y2, x1, y1, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw horizontal segment */&lt;br /&gt;			case 'H' : &lt;br /&gt;				scanf("%d %d %d %c", &amp;x1, &amp;x2, &amp;y1, &amp;color);&lt;br /&gt;				if (x2 &gt;= x1)&lt;br /&gt;					fillRectangle(x1, y1, x2, y1, color, image);&lt;br /&gt;				else&lt;br /&gt;					fillRectangle(x2, y1, x1, y1, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw rectangle */&lt;br /&gt;			case 'K' : &lt;br /&gt;				scanf("%d %d %d %d %c", &amp;x1, &amp;y1, &amp;x2, &amp;y2, &amp;color);&lt;br /&gt;				if (x1 &gt;= x2) {&lt;br /&gt;					tmp = x1;&lt;br /&gt;					x1 = x2;&lt;br /&gt;					x2 = tmp;&lt;br /&gt;				}&lt;br /&gt;				if (y1 &gt;= y2) {&lt;br /&gt;					tmp = y1;&lt;br /&gt;					y1 = y2;&lt;br /&gt;					y2 = tmp;&lt;br /&gt;				}&lt;br /&gt;				fillRectangle(x1, y1, x2, y2, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* fill */&lt;br /&gt;			case 'F' :&lt;br /&gt;				scanf("%d %d %c", &amp;x1, &amp;y1, &amp;color);&lt;br /&gt;				oldColor = image[y1][x1];&lt;br /&gt;				if (oldColor != color) {&lt;br /&gt;					fillRegion(x1, y1, oldColor, color, image);&lt;br /&gt;				}&lt;br /&gt;				break;&lt;br /&gt;&lt;br /&gt;			/* fill */&lt;br /&gt;			case 'S' :&lt;br /&gt;				scanf("%s", &amp;filename);&lt;br /&gt;				printf("%s\n", filename);&lt;br /&gt;				printImage(m, n, image);&lt;br /&gt;				break;			&lt;br /&gt;		&lt;br /&gt;			default: &lt;br /&gt;				break;&lt;br /&gt;		}		&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 18 Mar 2008 09:23:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5245</guid>
      <author>jmftrindade (Joana M. F. da Trindade)</author>
    </item>
    <item>
      <title>Graphical Plotter //Javascript Class</title>
      <link>http://snippets.dzone.com/posts/show/682</link>
      <description>&lt;a href="http://www.jsfromhell.com/dhtml/graphical-plotter"&gt;&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An unuseful thing to draw using javascript, it's slow as hell :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/dhtml/graphical-plotter [v1.0]&lt;br /&gt;&lt;br /&gt;Canvas = function(){&lt;br /&gt;	var o = this;&lt;br /&gt;	( o.penPos = { x: 0, y: 0 }, o.pixelSize = 10, o.pen = { style: "solid", size: 1, color: "#000" }, o.brush = { style: "solid", color: "#000" } );&lt;br /&gt;};&lt;br /&gt;with( { p: Canvas.prototype } ){&lt;br /&gt;	p.pixel = function( x, y, color ) {&lt;br /&gt;		var o = this, s = document.body.appendChild( document.createElement( "div" ) ).style;&lt;br /&gt;		return ( s.position = "absolute", s.width = ( o.pen.size * o.pixelSize ) + "px", s.height = ( o.pen.size * o.pixelSize ) + "px", s.fontSize = "1px", s.left = ( x * o.pixelSize ) + "px", s.top = ( y * o.pixelSize ) + "px", s.backgroundColor = color || o.pen.color, o );&lt;br /&gt;	};&lt;br /&gt;	p.line = function( x1, y1, x2, y2 ){&lt;br /&gt;		if( Math.abs( x1 - x2 ) &lt; Math.abs( y1 - y2 ) )&lt;br /&gt;			for( y = Math.min( y1, y2 ) - 1, x = Math.max( y1, y2 ); ++y &lt;= x; canvas.pixel( ( y * ( x1 - x2 ) - x1 * y2 + y1 * x2 ) / ( y1 - y2 ), y ) );&lt;br /&gt;		else&lt;br /&gt;			for( x = Math.min( x1, x2 ) - 1, y = Math.max( x1, x2 ); ++x &lt;= y; canvas.pixel( x, ( x * ( y1 - y2 ) - y1 * x2 + x1 * y2 ) / ( x1 - x2 ) ) );&lt;br /&gt;		return this;&lt;br /&gt;	};&lt;br /&gt;	p.arc = function( x, y, raio, startAngle, degrees ) {&lt;br /&gt;		for( degrees += startAngle; degrees --&gt; startAngle; this.pixel( Math.cos( degrees * Math.PI / 180 ) * raio + x, Math.sin( degrees * Math.PI / 180 ) * raio + y ) ); return this;&lt;br /&gt;	};&lt;br /&gt;	p.rectangle = function( x, y, width, height, rotation ){&lt;br /&gt;		return this.moveTo( x, y ).lineBy( 0, height ).lineBy( width, 0 ).lineBy( 0, -height ).lineBy( -width, 0 );&lt;br /&gt;	};&lt;br /&gt;	p.moveTo = function( x, y ){ var o = this; return ( o.penPos.x = x, o.penPos.y = y, o ); };&lt;br /&gt;	p.moveBy = function( x, y ){ var o = this; return o.moveTo( o.penPos.x + x, o.penPos.y + y ); };&lt;br /&gt;	p.lineTo = function( x, y ){ var o = this; return o.line( o.penPos.x, o.penPos.y, x, y ).moveTo( x, y ); };&lt;br /&gt;	p.lineBy = function( x, y ){ var o = this; return o.lineTo( o.penPos.x + x, o.penPos.y + y ); };&lt;br /&gt;	p.curveTo = function( cx, cy, x, y ){};&lt;br /&gt;	p.polyBezier = function( points ){};&lt;br /&gt;	p.path = function( points ){};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;canvas = new Canvas;&lt;br /&gt;&lt;br /&gt;canvas.pen.color = "#f00";&lt;br /&gt;canvas.rectangle( 30, 20, 20, 20 );&lt;br /&gt;canvas.pen.color = "#080";&lt;br /&gt;canvas.rectangle( 35, 25, 10, 10 );&lt;br /&gt;canvas.pen.color = "#008";&lt;br /&gt;canvas.arc( 50, 30, 10, 180, 270 );&lt;br /&gt;canvas.arc( 30, 30, 10, 0, 270 );&lt;br /&gt;canvas.pen.color = "#ff0";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 07:25:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/682</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
