<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: plotter code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 04:11:58 GMT</pubDate>
    <description>DZone Snippets: plotter code</description>
    <item>
      <title>Wave Plotter //Pascal Class</title>
      <link>http://snippets.dzone.com/posts/show/2201</link>
      <description>A wave plotter component that is able to draw sin, poly and squared lines, I used this in a osciloscope xD&lt;br /&gt;&lt;br /&gt;I didn't liked this code, but the draw part (TWaveShape.paint) is looking cool, I used the same "X" coord to draw the 3 kinds of lines :)&lt;br /&gt;&lt;br /&gt;I mean:&lt;br /&gt;&lt;br /&gt;loop(x){&lt;br /&gt;  case waveType of&lt;br /&gt;    wtSin: y := lala;&lt;br /&gt;    wtPoly: y := lele;&lt;br /&gt;    wtSqr: y := lili;&lt;br /&gt;  end;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;unit WavePlotter;&lt;br /&gt;&lt;br /&gt;interface&lt;br /&gt;&lt;br /&gt;uses&lt;br /&gt;  forms, dialogs, SysUtils, Classes, Controls, Graphics;&lt;br /&gt;&lt;br /&gt;const&lt;br /&gt;  PI2 = PI * 2;&lt;br /&gt;&lt;br /&gt;type&lt;br /&gt;  TWaveType = ( wtSqr, wtPoly, wtSin );&lt;br /&gt;&lt;br /&gt;  TWave = class( TPersistent )&lt;br /&gt;  public&lt;br /&gt;    waveType: TWaveType;&lt;br /&gt;    color: TColor;&lt;br /&gt;    offset: integer;&lt;br /&gt;    frequency, amplitude, volts, interval, gain: extended;&lt;br /&gt;&lt;br /&gt;    procedure AssignTo(Dest: TPersistent); override;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;  TWaveShape = class( TGraphicControl )&lt;br /&gt;  protected&lt;br /&gt;    fWaves: TList;&lt;br /&gt;    fBoxSize: integer;&lt;br /&gt;    fLineColor: TColor;&lt;br /&gt;&lt;br /&gt;    function getWave(const index: integer): TWave;&lt;br /&gt;    procedure setBoxSize(const Value: integer);&lt;br /&gt;&lt;br /&gt;    function getBackgroundColor: TColor;&lt;br /&gt;    procedure setBackgroundColor(const Value: TColor);&lt;br /&gt;    procedure setLineColor(const Value: TColor);&lt;br /&gt;&lt;br /&gt;  public&lt;br /&gt;    constructor create( AOwner: TComponent ); override;&lt;br /&gt;    destructor destroy; override;&lt;br /&gt;&lt;br /&gt;    procedure clear;&lt;br /&gt;    procedure delete( const index: integer );&lt;br /&gt;&lt;br /&gt;    function add: integer;&lt;br /&gt;&lt;br /&gt;    property waves[ const index: integer]: TWave read GetWave;&lt;br /&gt;&lt;br /&gt;  published&lt;br /&gt;    procedure paint; override;&lt;br /&gt;    property boxSize: integer read fBoxSize write setBoxSize;&lt;br /&gt;    property backgroundColor: TColor read getBackgroundColor write setBackgroundColor;&lt;br /&gt;    property lineColor: TColor read fLineColor write setLineColor;&lt;br /&gt;&lt;br /&gt;    //inherited&lt;br /&gt;    //property Canvas;&lt;br /&gt;    property Align;&lt;br /&gt;    property Anchors;&lt;br /&gt;    property Constraints;&lt;br /&gt;    property DragCursor;&lt;br /&gt;    property DragKind;&lt;br /&gt;    property DragMode;&lt;br /&gt;    property Enabled;&lt;br /&gt;    //property Font;&lt;br /&gt;    property ParentColor;&lt;br /&gt;    //property ParentFont;&lt;br /&gt;    property ParentShowHint;&lt;br /&gt;    property PopupMenu;&lt;br /&gt;    property ShowHint;&lt;br /&gt;    property Visible;&lt;br /&gt;    property OnClick;&lt;br /&gt;    property OnContextPopup;&lt;br /&gt;    property OnDblClick;&lt;br /&gt;    property OnDragDrop;&lt;br /&gt;    property OnDragOver;&lt;br /&gt;    property OnEndDock;&lt;br /&gt;    property OnEndDrag;&lt;br /&gt;    property OnMouseDown;&lt;br /&gt;    property OnMouseMove;&lt;br /&gt;    property OnMouseUp;&lt;br /&gt;    property OnStartDock;&lt;br /&gt;    property OnStartDrag;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;implementation&lt;br /&gt;&lt;br /&gt;function max( const a, b: integer ): integer;&lt;br /&gt;begin&lt;br /&gt;  if a &gt; b then&lt;br /&gt;    result := a&lt;br /&gt;  else&lt;br /&gt;    result := b;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;{ TWaveShape }&lt;br /&gt;&lt;br /&gt;function TWaveShape.add: integer;&lt;br /&gt;begin&lt;br /&gt;  result := fWaves.add( TWave.Create );&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.clear;&lt;br /&gt;begin&lt;br /&gt;  while fWaves.count &gt; 0 do begin&lt;br /&gt;    TWave( fWaves[0] ).free;&lt;br /&gt;    fWaves.delete( 0 );&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;constructor TWaveShape.create(AOwner: TComponent);&lt;br /&gt;begin&lt;br /&gt;  inherited;&lt;br /&gt;  fWaves := TList.create;&lt;br /&gt;  fLineColor := clGray;&lt;br /&gt;  color := clBtnFace;&lt;br /&gt;  fBoxSize := 50;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.delete(const index: integer);&lt;br /&gt;begin&lt;br /&gt;  if ( index &gt; -1 ) and ( index &lt; fWaves.count ) then begin&lt;br /&gt;    TWave( fWaves[index] ).free;&lt;br /&gt;    fWaves.delete( index );&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;destructor TWaveShape.destroy;&lt;br /&gt;begin&lt;br /&gt;  fWaves.free;&lt;br /&gt;  inherited;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;function TWaveShape.getBackgroundColor: TColor;&lt;br /&gt;begin&lt;br /&gt;  result := color;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;function TWaveShape.getWave(const index: integer): TWave;&lt;br /&gt;begin&lt;br /&gt;  result := nil;&lt;br /&gt;  if ( index &gt; -1 ) and ( index &lt; fWaves.count ) then&lt;br /&gt;    result := TWave( fWaves[ index ] );&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.paint;&lt;br /&gt;var&lt;br /&gt;  k, x: integer;&lt;br /&gt;  lastX, lastY: array of integer;&lt;br /&gt;  y: extended;&lt;br /&gt;begin&lt;br /&gt;  if not enabled then&lt;br /&gt;    exit;&lt;br /&gt;  canvas.brush.color := color;&lt;br /&gt;  canvas.fillRect( clientRect );&lt;br /&gt;&lt;br /&gt;  setLength( lastY, fWaves.count );&lt;br /&gt;  setLength( lastX, fWaves.count );&lt;br /&gt;  for k := 0 to high( lastY ) do begin&lt;br /&gt;    lastY[k] := clientHeight div 2 + waves[k].offset;&lt;br /&gt;    lastX[k] := 0;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;  for x := 0 to max( clientWidth, clientHeight ) do begin&lt;br /&gt;    with canvas do begin&lt;br /&gt;      pen.color := fLineColor;&lt;br /&gt;      pen.width := 1;&lt;br /&gt;      pen.style := psDot;&lt;br /&gt;&lt;br /&gt;      if x mod fBoxSize = 0 then begin&lt;br /&gt;        moveTo( 0, x + trunc( frac( clientHeight / 2 / fBoxSize ) * fBoxSize ) );&lt;br /&gt;        lineTo( clientWidth, x + trunc( frac( clientHeight / 2 / fBoxSize ) * fBoxSize ) );&lt;br /&gt;&lt;br /&gt;        moveTo( x, 0 );&lt;br /&gt;        lineTo( x, clientHeight );&lt;br /&gt;      end;&lt;br /&gt;&lt;br /&gt;      for k := 0 to fWaves.count - 1 do begin&lt;br /&gt;        with waves[k] do begin&lt;br /&gt;          pen.color := color;&lt;br /&gt;          pen.width := 1;&lt;br /&gt;          pen.style := psSolid;&lt;br /&gt;&lt;br /&gt;          y := pi*k + PI2 * x * interval / fBoxSize * frequency;&lt;br /&gt;&lt;br /&gt;          case waveType of&lt;br /&gt;            wtSin:&lt;br /&gt;              y := sin( y );&lt;br /&gt;            wtPoly: begin&lt;br /&gt;              y := frac( y / PI2 );&lt;br /&gt;              if y &lt;= 0.25 then&lt;br /&gt;                y := y / 0.25&lt;br /&gt;              else if y &lt;= 0.75 then&lt;br /&gt;                y := ( -y + 0.5 ) / 0.25&lt;br /&gt;              else&lt;br /&gt;                y := ( y - 1 ) / 0.25;&lt;br /&gt;            end;&lt;br /&gt;            wtSqr: begin&lt;br /&gt;              if frac( y / PI2 ) &lt;= 0.5 then&lt;br /&gt;                y := 1&lt;br /&gt;              else&lt;br /&gt;                y := -1;&lt;br /&gt;            end;&lt;br /&gt;          end;&lt;br /&gt;          y := ( y * ( fBoxSize / volts ) * amplitude * gain ) + clientHeight / 2 + offset;&lt;br /&gt;          moveTo( lastX[k], lastY[k] );&lt;br /&gt;          lastX[k] := x;&lt;br /&gt;          lastY[k] := trunc( y );&lt;br /&gt;          lineTo( x, lastY[k] );&lt;br /&gt;        end;&lt;br /&gt;      end;&lt;br /&gt;    end;&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.setBackgroundColor(const Value: TColor);&lt;br /&gt;begin&lt;br /&gt;  color := Value;&lt;br /&gt;  Invalidate;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.setBoxSize(const Value: integer);&lt;br /&gt;begin&lt;br /&gt;  fBoxSize := Value;&lt;br /&gt;  invalidate;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;procedure TWaveShape.setLineColor(const Value: TColor);&lt;br /&gt;begin&lt;br /&gt;  fLineColor := Value;&lt;br /&gt;  Invalidate;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;{ TWave }&lt;br /&gt;&lt;br /&gt;procedure TWave.AssignTo(Dest: TPersistent);&lt;br /&gt;begin&lt;br /&gt;  if Dest.ClassType &lt;&gt; TWave then&lt;br /&gt;    inherited;&lt;br /&gt;  with TWave( Dest ) do begin&lt;br /&gt;    waveType := self.waveType;&lt;br /&gt;    color := self.color;&lt;br /&gt;    offset := self.offset;&lt;br /&gt;    frequency := self.frequency;&lt;br /&gt;    amplitude := self.amplitude;&lt;br /&gt;    volts := self.volts;&lt;br /&gt;    interval := self.interval;&lt;br /&gt;    gain := self.gain;&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;end.&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Jun 2006 19:28:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2201</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</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>
    <item>
      <title>pascal triangle plotter XD</title>
      <link>http://snippets.dzone.com/posts/show/440</link>
      <description>&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;conio.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAX 16&lt;br /&gt;&lt;br /&gt;int main () {&lt;br /&gt;	int unsigned vetor[MAX], grau, i = 0, j, top, left;&lt;br /&gt;&lt;br /&gt;	clrscr();&lt;br /&gt;&lt;br /&gt;	printf( "Dado o grau, printar o triangulo de pascal\n" );&lt;br /&gt;&lt;br /&gt;	while( grau &gt; MAX &amp;&amp; printf( "Digite um numero entre 0 e %d para o grau: ", MAX ) &amp;&amp; scanf( "%u", &amp;grau ) );&lt;br /&gt;	while( i++ &lt; grau &amp;&amp; !( j = 0 ) &amp;&amp; printf( "\n" ) )&lt;br /&gt;		while( j &lt; i &amp;&amp; ( j == 0 &amp;&amp; ( top = left = vetor[j] = 1 ) ? 1 : j &gt; 0 &amp;&amp; j &lt; i-1 &amp;&amp; ( top = vetor[j] ) &amp;&amp; ( vetor[j] = left + top ) &amp;&amp; ( left = top ) ? 1 : ( vetor[j] = 1 ) ) &amp;&amp; printf( "%-5d", vetor[j] ) &amp;&amp; ++j );&lt;br /&gt;	getch();&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jul 2005 03:59:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/440</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
