Archive

Posts Tagged ‘PHP’

3D vector distance calculation

July 24th, 2009 Athernar No comments

Recently I decided to revisit Vectors. Being something that while I covered in A-Level Physics,  but never really grasped beyond the basics.

So in the process of messing around with vectors, I wrote a small 3D vector distance calculator in PHP, which I then proceeded to port to C++ for fun. Code is below, and I’ll release the EXE if someone wants it, not really useful unless you want a quick(ish) way of getting distances I suppose.

Enjoy:

< ?php
 function distance($a, $b)
 {
  return sqrt(pow($a[0] - $b[0], 2) + pow($a[1] - $b[1], 2) + pow($a[2] - $b[2], 2));
 }

 $a = array(-1, 0, 6);
 $b = array(5, 2, 10);
 
 echo distance($a, $b); 
?>
#include <iostream>
#include <math .h>

using namespace std;

float distance(float &Bx, float &By, float &Bz)
{
 float Ax = 0.0f, Ay = 0.0f, Az = 0.0f;
 return sqrt(pow(Ax - Bx, 2) + pow(Ay - By, 2) + pow(Az - Bz, 2)); 
}

int main()
{
 float x, y, z;
 bool loop;

 do
 {
  cout < < "Enter vector:" << endl << "X: ";
  cin >> x;
  cout < < "Y: ";
  cin >> y;
  cout < < "Z: ";
  cin >> z;

  cout < < "Result: " << distance(x, y, z) << endl << "Again? 1/0: ";
  cin >> loop;
 } while(loop == true); 

 return 0;
}

Download: EXE ~10 KiB

Categories: Coding Tags: ,

DAC Coding and Syntax Highlighting

July 23rd, 2009 Athernar No comments

Quick update to say I added a Syntax-Highlighting plugin to the Blog to aid your reading of my code.

In other news however, me and n00berific have made a fair bit of headway with DAC as of late, we’ve got all the HTML entities working while managing to strip out a few server-side functions in the process, I’m looking into optimising filesize by CSS-based newline handling, but more on that later.

For the mean time, here is DAC’s sendmessage.php for you to take a look through.

EDIT: Updated the code snippet slightly

< ?php
 if (!empty($_POST["message"]) && !empty($_POST["username"]))
 {
  date_default_timezone_set("Europe/London");
  
  if (stripos($_POST["message"], "/me") !== false)
   $msg = "<span class='name'>" . strftime("[%H:%M] ") . htmlentities($_POST["username"]) . ": " . htmlentities(str_ireplace("/me", NULL, $_POST["message"])) . "\r\n";
  else
   $msg = "<span class='name'>" . strftime("[%H:%M] ") . htmlentities($_POST["username"]) . ":</span> " . htmlentities($_POST["message"]) . "\r\n"; 
   
  $chat = file("chat.txt");
  if (count($chat) >= 100)
  {  
   $chat = array_slice($chat, 1, NULL, true);
   file_put_contents("chat.txt", implode($chat) . $msg);
  }
  else
   file_put_contents("chat.txt", $msg, FILE_APPEND); 
   
  echo implode($chat) . $msg;
 }
 else
  echo file_get_contents("chat.txt");
?>
Categories: Coding Tags: ,

Site Reorg

July 12th, 2009 Athernar No comments

Going to be reorganising the entire website over the next couple of days for simplicity and ease-of-bookmarking.

I also have planned a few updates to the site homepage, replacing the unused Ather-Tech twitter with a RSS feed from this blog.

Along with that the topics I will cover on this may diversify in the future, so keep an eye out.

Categories: Misc Tags: ,

An update.

July 3rd, 2009 Athernar No comments

I have been neglecting this blog for far too long, and now that I have some more thoughts I’d though I would sit down and write out an update.

Code-wise, alot has changed since the last post. I completed my refactor of AthList and then went onto AthLoad which was completed fairly quickly. After that I did some refresh work on the site homepage, feature additions to both AthList and AthLoad, and I completed a commissioned app for a friend of mine.

As of late however work has been slow, I’ve been plotting out a major rewrite of AthList and AthLoad, which involves essentially stripping out all code that is not related to the class’s specific functionality, this effort has generated a 3rd class, called AthLog (User Log-in handler, original naming eh?) which along with it’s sibling Ath- classes will make up a reusable class library called AthX. Although this is somewhat pushed to the side until my host updates to PHP 5.3.

Recently I’ve been working with n00berific on a project we affectionately call “Dumpass Chat” (Don’t ask) which is coming along nicely. noob is handling the JScript aspect while I write the XHTML and CSS, the PHP is a joint effort. You can check out DAC at http://chat.ather-tech.net/

If you’ve commented on here, then I apologise as in my haste to erase the bulk of spam I pretty much erased everything. I’ll try and keep a close eye on the comments in the future.

 

So in ending, I hope to have some new code snippets up soon, probably from AthX and/or DAC. Until then!

Categories: Coding Tags: , , ,

Work continues.

December 15th, 2008 Athernar No comments

Been doing a bit more work on www.ather-tech.net/refactor (UPDATE: 2.0 went live, link is now broken) as of late, for the most part the Class is “Method-Complete” although that may change. All that remains now is to clean up the code, and bugfix up to a final version (2.0.0).

On the Index side of things, the code is a tad more complex than the basic example I demonstrated previously:

< ?php
 include('refactor_test.php');
 $AthList = new AthList($_GET["path"]);
 
 $AthList->SetExclude(".,..,error_log");
 $list = $AthList->GetList(false);
 
 $regex = array (
  0 => "[\{FDATA\}]",
  1 => "[\{VER\}]",
  2 => "[\{FPATH\}]"
 );
 $replacement = array (
  0 => $AthList->PrintList($list),
  1 => $AthList->GetVersion(),
  2 => $AthList->GetPath()
 );
 
 $file = fopen("markup.htm", "r"); 
 $markup = fread($file, filesize("markup.htm"));
 $markup = preg_replace($regex, $replacement, $markup);
 fclose($file);
 
 echo $markup;
?>

The extra code is just to load my markup (XHTML 1.0) file, and parse through with preg_replace() and two indexed arrays to insert the relevant data pulled from the class into the markup framework. I might look into integrating this functionality into the AthList class and providing two methods for both Basic and Rich output.

Any questions or suggestions are welcome.

Categories: Coding Tags: ,

Test post/AthList

December 11th, 2008 Athernar No comments

This new wordpress release is all flashy and neat isn’t it.

In other news however, my refactoring of AthList is making nice progress. The basics of the new class are working and getting it all working has allowed me to take a new fresher look at the code, and thus I’ve been able to make a couple of optimisations.

You can see my current work-in-progress at www.ather-tech.net/refactor, it is currently missing a couple of features found in the 1.x branch such as “Up” navigation and File-type icons. My plan is to get “Up” functionality working first, and then the File-type icons when n00berific can spare some time. *nudge-nudge*

The current code to return a basic list is simple:

<?php
 include(‘refactor_test.php’);
 $AthList = new AthList($_GET["path"]);
 
 $AthList->SetExclude(“.,..,error_log”);
 $list = $AthList->GetList(true); 
 echo $AthList->PrintList($list);
?>

I shall be providing documentation of the AthList class once I’ve completed implementing all of the current 1.x features in 2.x, until then.

Happy coding!

Categories: Coding Tags: ,
Easy AdSense by Unreal