A plot to plot a line

I had no work one day due to a worm attack on our servers, thus a plot to plot a line on a graph was hatched in my empty mind. These files show you, how to plot a line using 3 tools, Loop, table and text
Recall the Cartesian co-ordinate system; a line segment has 2 points with a starting point at (x1,y1)
and an ending point at (x2,y2).



This form asks you for those two points in your Cartesian system.
I have limited my systems from -20 to +20 on the X- and Y- Axes, as you will see on the next page.

INDEX.CFM (or call it anything you want): This is the page the hatches the plot!

<cfform action="plot.cfm" method="POST" enablecab="Yes" name="plot">
1<sup>st</sup> Point Co-ordinates (x<sub>1</sub>,y<sub>1</sub>)<br>
X: <cfinput type="Text" name="xa1" validate="integer" required="Yes">
Y: <cfinput type="Text" name="yb1" validate="integer" required="Yes">
<br>

2<sup>nd</sup> Point Co-ordinates (x<sub>2</sub>,y<sub>2</sub>)<br>
X: <cfinput type="Text" name="xa2" validate="integer" required="Yes">
Y: <cfinput type="Text" name="yb2" validate="integer" required="Yes"><br>
<hr>

<input type="submit" name="Plot" value="Plot It">
</cfform>






PLOT.CFM (Where the plot is executed)

<cfset xp = 20>
<cfset yp =
20>

The above 2 cfset statements set positive side extents (xp is x-postive and yp means y-positive). I have set them to 20. But for a finer graph you can increase or decrease them or let user control them via a form.
<cfset x0=0>
<cfset y0 =
0>

The above 2 cfset statements set the origin at 0,0
<cfset xn=-#xp#>
<cfset yn=-#yp#>

The above 2 cfset statements set the negative extents. Just because I wanted to make this graph look square with origin at the center or where the diagonals meet.

<cfset a1 = '#form.xa1#'>
<cfset b1 =
'#form.yb1#'>

<cfset a2 = '#form.xa2#'>
<cfset b2 =
'#form.yb2#'>

The above 4 cfset statements use the numbers provided to them from the form on index.cfm !
<cfset run = (#a2#)-(#a1#)>
<cfset rise =
(#b2#)-(#b1#)>
This is how you calculate slope of a line. Given 2 point on a Cartesian co-ordinate system, slope is (y2-y1)/(x2-x1). Denominator cannot be 0, or we get an error. In case it is then there is ?no slope to that line?. We can still plot the line, or points through which segment starts and ends.

<cfif run neq 0>
    <cfset slope =
(#rise#/#run#)>
<cfelse>
    <cfset slope=
''>
</cfif>

The above statements just calculate the slope and output the result below;
Slope = <cfoutput>#slope#</cfoutput>


Once that?s done, I wanted to plot this line without using arrays or making it too complex.
One thing to be understood is how a table tag is compiled. Inside a <table> tag, you specify rows 1st (this is your Y-Axis) and then the columns (X-Axis) in each row.
Second thing to be noted is, for a good graph, you have to control the size of each square on your graph. So we resort to stylesheets thus;
<!---
<td style="width:15px;height:15px;font-size:10px;" align="center" valign="middle">
--- >






A simple table tag will have a structure like;
<table>
<tr>
<!--- y-axis --- >
<td></td>
<!--- x-axis --- >
</tr>
</table>


So loop <tr> (in my case) form +20 to ?20. Not that the step is ?1.
Because each column is defined after each row, nest another loop or <td> and loop it from -20 to +20 and let step be default =1. Your graph is ready!

<table>
<cfloop index="y" from="#yp#" to="#yn#" step='-1'>

<tr bgcolor="efefef" style="border-width:1px; border-style:thin;">

<cfloop index="x" from="#xn#" to="#xp#" step='1'>

<cfset x_cords =x-y>
<cfset y_cords =
y-x>
<!--- This is how you set co-ordinates to appear in the center of your graph --- >
<cfset ptx = x-a1>
<cfset pty =
y-b1>
<cfif ptx
gt 0 or ptx lt 0>
    <cfset s =
pty/ptx>
<cfelse>
<cfset s=0>
</cfif>

<!--- if you use x as index of loop for x-axis, and subtract first point x1 (in my case a1), you will get some number for ?some run?. The if statements below, output ?x? for every point where the ?some run? is equal to actual run calculated above for (a2-a1). And if you use Y for the index and subtract point y1, you will get ?some rise?. Again out put a character (x in my case) for if conditions are suitable. You could have used X2 and Y2 (or a2 and b2) instead of the x1 and y1. and flipped number is subtraction to B2-Y or A2-X. The principal is the same.
Say the Y loop outputs 1st point = 20 and you specified a1 = 10 and a2 = 20, then;
B2-B1 = 10 And Y-B1= 10
So common point, output an X in that cell. Simple?
--- >

<td style="width:15px;height:15px;font-size:10px;" align="center" valign="middle">

<cfif x_cords eq x>
    <cfoutput>
        #x_cords#
    </cfoutput>
<cfelseif y_cords
eq y>
    <cfoutput>
        #y_cords#
    </cfoutput>
    <!--- The above statements output x and y axes and scale them--->

<cfelseif rise eq 0>
    <!--- Recall from above, if rise is 0 then slope is 0 --- >

<cfelseif s eq slope>
    <b>x</b>
    <!--- This is your line made up of all Xs --- >
<cfelseif x eq a1 and y eq b1>
    <b>o</b>
    <!--- I used an O for start and end points--- >
<cfelseif x eq a2 and y eq b2>
    <b>o</b>
</cfif>
</td>
</cfloop>
</tr>
</cfloop>
</table>
<!---
I have noticed great plots for slopes with 1 to 1.5 and their (-)ve counterparts. Lines with no slope plots in just 2 points. Here is an example of a great plot with points A (5,-8) and B(-8,5), with slope ?1 (obviously). But you can play with the extents of the graphs and origins to get a plot.

The next time our servers are down I am going to try to plot curves and ultimately try to get an Edge-worth box to plot. If you get to it first, let me in on your plot!!!
--->



All ColdFusion Tutorials By Author: Anang A Phatak
  • A DataSet just like VB.Net
    This tutorial shows you how to create a "dataset" just like the one in VB.Net In VB.Net you would create a dataset with "edit" button in an extra column. Once you click "edit", you get an option to "update", "delete" or "cancel edit mode" This is just like a cfgrid tag. Although a cfgrid tag lets you bulk insert, bulk update or bulk delete, the dataset object does it one by one. But cfgrid is slower, and may give users Java errors, depending on their browser settings.
    Author: Anang A Phatak
    Views: 14,477
    Posted Date: Wednesday, November 17, 2004
  • A Mp3 Streaming Server
    This is a small application that shows you how to create an MP3 streaming server.
    Author: Anang A Phatak
    Views: 11,643
    Posted Date: Monday, November 8, 2004
  • A plot to plot a line
    I had no work one day due to a worm attack on our servers, thus a plot to plot a line on a graph was hatched in my empty mind. These files show you, how to plot a line using no database, no java, no long wait times for aplet loading, just 3 tools, Loop, table and text.
    Author: Anang A Phatak
    Views: 7,553
    Posted Date: Thursday, May 6, 2004
  • A random password generator
    RANDOM PASSWORD GENERATOR SCRIPT ! I know there is a random password tutorial here already. This is just another way to do the same. I think this is a little easier to understand. Refresh it to generate a new password string everytime !
    Author: Anang A Phatak
    Views: 9,243
    Posted Date: Monday, May 24, 2004
  • Advanced Calculator
    I have posted a "Basic Calculator" tutorial here. That was more like a representation of how you would calculate with a paper and a pencil. You provide INPUT A then a MATHEMATICAL OPERATION like a "+" or a "-" and then an INPUT B. This is more a represntation of how you would use a regular hand-held calculator complete with buttons for NUMBERS, OPERATIONS and CLEAR TEXT.
    Author: Anang A Phatak
    Views: 8,458
    Posted Date: Friday, June 18, 2004
  • Automatic Form Generator
    This is not a tutorial as such, more like an application that you can put in a directory. It could boring if you use the CF editor, to pick "cfform" fill its attributes, then pick "cfinputs" one by one. fill out those attributes... one by one, then change tabs and pick the "submit" button... so on and so forth. Even if you code in a note pad, it might get lengthy to code individual element. Wouldn't it be nicer to code all these elements at once, then just copy the code and paste it in your editor?
    Author: Anang A Phatak
    Views: 8,881
    Posted Date: Thursday, May 20, 2004
  • Automatically Query To CFM
    This is a custom tag application. The cf_QueryRender custom tag takes your query arguments and gives you a final page table and all...
    Author: Anang A Phatak
    Views: 10,889
    Posted Date: Friday, October 29, 2004
  • Breaking down your query results into pages (Paging Tutorial)
    I havent come across a "paging" tutorial on this site. I know there are JavaScripts available that help you achieve this, and the DataSet object in VB.Net comes with paging. All you do is "enable paging". But how do you do it in ColdFusion ?
    Author: Anang A Phatak
    Views: 14,461
    Posted Date: Tuesday, November 16, 2004
  • Breaking down your query results into pages (Paging Tutorial) Part-II
    This is an extension to my last tutorial "Breaking down your query results into pages (Paging Tutorial)" which is posted here on www.easycfm.com In the last tutorial, you could retrieve a dataset with a , then use a technique to seperate the results over several pages. It simply ; - took the total "recordCount" - divided that with the "number of records per page" - then displayed the number of pages at the bottom of the table. This is a little more sophisticated than that. Read on...
    Author: Anang A Phatak
    Views: 7,297
    Posted Date: Wednesday, January 11, 2006
  • ColdFusion MX 6.1 Installation on Linux (Ubuntu -- Hoary Hedgehog)
    I have tried hoards of websites on how to install coldfusion on Fedora Core 3 with apache webserver. For some reason the connectors always failed. I had "Ubuntu" on my laptop, basically because "acpi" suspend/hibernate actually works. I decided I might try to install CF there to find out what was going wrong. Surprisingly everything worked like a charm. Make sure you use "apt-get install apache2" before you try this. BEST OF LUCK ....
    Author: Anang A Phatak
    Views: 8,907
    Posted Date: Tuesday, May 10, 2005
  • Dynamic textbox and progress bar for your pages
    The principle of this tutorial is similar to "Dynamic time and date for your pages" tutorial. Except that this one generates messages, and that one updated time. Read on, you will get the hang of it....
    Author: Anang A Phatak
    Views: 11,098
    Posted Date: Thursday, May 19, 2005
  • Dynamic time and date for your pages
    Have you seen the "www.EasyCFM" page closely? On the main page, top right, there is a place for time, and top left a place for day-date. Ever wonder how Pablo does it ? This is not a ColdFusion tutorial. Its JavaScript.
    Author: Anang A Phatak
    Views: 9,130
    Posted Date: Wednesday, January 5, 2005
  • Getting ColdFusion Studio for Linux
    I like using HomeSite+ for windows, and I am getting used to Dreamweaver Mx. But I still need something just as good for Linux. For some reason, I couldnt get "wine and Dreamweaver Mx" to work. So I "Googled" a bit and stumbled upon Eclipse and cfEclipse. Here is how to set it up.
    Author: Anang A Phatak
    Views: 10,169
    Posted Date: Wednesday, May 25, 2005