Thursday, January 29, 2009

Angle of Vectors by Dot Product in 3D space


Dot product of two vectors with the same number of components shows the angular relationship between two vectors.
A = Ax(i) + Ay(j) + Az(k)
B = Bx(i) + By(j) + Bz(k)

(i), (j), and (j) in above vectors represent unit vectors which are belong to x, y, and z coordinate direction and projection of vector A along the x, y, and z axis are presented by Ax, Ay, and Az.

The magnitude f a vector in 3D X-Y-Z coordinate system can be calculated as following:
|A| = Sqrt( Ax * Ax + Ay * Ay + Az * Az )

...
vecAlen = Math.Sqrt( Math.Pow(Ax, 2.0) + Math.Pow(Ay, 2.0) + Math.Pow(Az, 2.0) )
...

The dot product of vectors A and B is equal to vector A magnitude multiply by vector B magnitude multiply by cosine of angle between the two vectors (teta).
A.B = |A| * |B| * Cos(teta)

it results the following facts too:
(A.B = B.A) and also we know (A.A = |A| * |A|) because teta is 0 and Cos(0) is equal to 1.

In 3D X-Y-Z rectangular coordinate system the dot product can be evaluated from the relation:
A.B = Ax * Bx + Ay * By + Az * By

The proof of above relation comes from the facts that related to unit vectors in rectangular 3D X-Y-Z coordinate system which presented by 90 degree angle between Axis directions:
(i) . (j) = (i) . (k) = (j) . (k) = 0
(i) . (i) = (j) . (j) = (k) . (k) = 1

Now if we want to calculate angle between the A and B are unit vectors (vectors with magnitude of 1), then simply we have:
A.B = Cos(teta)
teta = arcCos( A . B )

The code can be something like this:
public struct vector
{
   public double Vx;
   public double Vy;
   public double Vz;
}
...
public double vecsAngle(vector A, vector B)
{
 double dotPrdct = A.Vx*B.Vx + A.Vy*B.Vy + A.Vz*B.Vz;
 return Math.Acos( dotPrdct ) * 180 / Math.PI;
}

Share/Bookmark

Tuesday, January 27, 2009

SQL - Recursive Queries by CTE

CTE (Common Table Expression) is very useful when we want to perform recursion by defining a self referencing query. In Northwind Database sample (downloadable from CodePlex web pages) Employees table has a column with title of ReportsTo which refers to EmployeeID of another Employee record in current Employee row, it's the manager. The exception is for the managers who their ReportsTo column is NULL (They are lucky people who don't need to report to any body, watch out, it shows you are responsible not them!). Then the managers are sitting in Level 1 and employees are in next levels. Then employees report to their manager who are in upper level. It shows the table has a reference to itself, it's a join back that is possible to implement by utilizing power of recursive connection.

To cover all the sub levels and show Employees and their managers, we need to UNION result set of multi levels all together and go deep inside till find all requested info. Each time that the query needs to go one level deeper, the Level column adds by 1 to show the correct level of employee in organization diagram.

WITH Emp (EmpID, EmpName, ManagerID,
ManagerName, [EmployeeLevel] )
AS
(
SELECT EmpTbl.EmployeeID,
EmpTbl.LastName,
EmpTbl.ReportsTo,
EmpTbl.FirstName+SPACE(1)+EmpTbl.LastName, 1
FROM [Northwind].[dbo].[Employees] as EmpTbl
WHERE ReportsTo IS NULL
UNION ALL
SELECT EmpTbl.EmployeeID,
EmpTbl.LastName,
EmpTbl.ReportsTo,
Mgr.FirstName+SPACE(1)+Mgr.LastName,
[EmployeeLevel] + 1
FROM [Northwind].[dbo].[Employees] as EmpTbl
JOIN Emp
ON Emp.EmpID = EmpTbl.ReportsTO
JOIN [Northwind].[dbo].[Employees] as Mgr
ON EmpTbl.ReportsTO = Mgr.EmployeeID
)
SELECT EmpID,
EmpName,
ManagerID,
ManagerName,
[EmployeeLevel]
FROM Emp

Output:

2 Fuller NULL Andrew Fuller 1
1 Davolio 2 Andrew Fuller 2
3 Leverling 2 Andrew Fuller 2
4 Peacock 2 Andrew Fuller 2
5 Buchanan 2 Andrew Fuller 2
8 Callahan 2 Andrew Fuller 2
6 Suyama 5 Steven Buchanan 3
7 King 5 Steven Buchanan 3
9 Dodsworth 5 Steven Buchanan 3

Share/Bookmark

Monday, January 26, 2009

Free - Taskbar Button Manager (Innovative Solutions)

If you are looking for a utility to make you able to arrange your buttons on windows taskbar in way which you want and just by drag n drop the buttons, then go to load this perfect utility:
Taskbar Button Manager

It's free and works on Windows XP, after 30 days it needs a registration code which comes also for free.

I was looking for it, found it, and now I enjoy having it on my system.
Share/Bookmark

Free Virus Scanners

Free Virus Scanners

http://free.avg.com/
http://www.kaspersky.com/virusscanner
http://security.symantec.com
http://us.mcafee.com/root/mfs/scan.asp?affid=56
http://www.bitdefender.com/scan8
http://onecare.live.com/site/en-us/default.htm
http://www.eset.com/onlinescan/
http://www.avast.com/eng/download-avast-home.html
http://ca.com/securityadvisor/virusinfo/scan.aspx
http://www.ewido.net/en/onlinescan
http://www.pandasecurity.com/homeusers/solutions/activescan
http://www.trendsecure.com/portal/en-US/tools/security_tools/housecall
http://www.clamwin.com/
http://www.clamav.net/
http://www.free-av.com/
http://www.radialpoint.net/home/
http://www.f-prot.com/ (Trial)
CyberDefender Early Detection Center - Trial Edition
Share/Bookmark

Sunday, January 25, 2009

The Internet browser Embedded Objects

Users can view non-HTML contents by embeded objects. For example to watch a flash animation, listen to a RealAudio audio, watch a QuickTime movie or many other non-HTML documents or media entities, a browser need to take advantage of embedded objects. It's possible to check client browser to see if required embedded object is installed or not.

Internet Explorer uses Microsoft ActiveX components to load other applications and support embedded objects and FireFox uses the Netscape Plugin Application Programming Interface (NPAPI) system. FireFox does not officially support ActiveX. (Mozilla and ActiveX)

Which properties keep the name of available embedded objects in the Internet browser?

The document.embeds[] array contains a list of all the objects embedded in a document which are applied by the <OBECT> tag for IE and the <EMBED> tag in Mozilla.

The document.applets[] works for <APPLET> tag in the web page codes and contains all the applets embedded in a document as Java Applet although <OBJECT> and <EMBED> are recommended instead of using <APPLET>.

The navigator.plugins[] array contains all the plug-ins that Mozilla supports like Adobe Acrobat.

The navigator.mimeTypes[] array contains all the MIME types supported by Mozilla. MIME is Multipurpose Internet Mail Extension, It related to the file types that Mozilla can understand and display. MIME is a component for communication protocols (HTTP) and also it defines methods for transferring binary data in ASCII text format as a message and more.

The navigator.plugins[] and navigator.mimeTypes[] arrays in IE are null cuz IE uses ActiveX to implement embedded objects instead of plug-ins, then in IE document.embeds[] array detects embedded contents.
Share/Bookmark

Saturday, January 24, 2009

How to find referrer page (JavaScript)

The referrer page is the page that user loaded before loading your web page. It's useful if you are interested to find visitors of you web page came from which URL to see your web pages.

Simply enough, the referrer property of the document object, identifies the referrer page URL.

If the value of document.referrer is blank ("") , then it shows user loaded up your page directly by typing its URL into the browser address field.

Just be notified that the value of document.referrer is blank if you test an HTML page locally in your machine.

if (document.referrer != "") {
   document.writeln("Ref page: " + document.referrer);
}
else
{
   document.writeln("Page loaded up from fresh.");
}

Share/Bookmark

Free - Download Windows 7 Beta CPP

If you are eager to try the Windows 7 Beta Customer Preview Program, go for the following URL (You can find a lot about it over there):
http://technet.microsoft.com/en-ca/windows/dd361745.aspx

Mark Russinovich: Inside Windows 7
http://channel9.msdn.com/shows/Going%20Deep/Mark-Russinovich-Inside-Windows-7/

One very important change in the Windows 7 kernel is the dismantling of the dispatcher spin lock and redesign and implementation of its functionality. The direct result of the reworking of the dispatcher spin lock is that Windows 7 can scale to 256 processors.
Share/Bookmark

How to determine the version of .NET framework in your system - Two

Internet Explorer DOM (Document Object Model) has a navigator object and there is a userAgent properties which shows browser application name and also version of .NET Framework which installed on your system. To query this property (userAgent) simply enter the following line in address bar, the alert method in javascript will show the result in a message window.

javascript:alert(navigator.userAgent)


Share/Bookmark

Friday, January 23, 2009

How to determine the version of .NET framework in your system - One

Locate the following folder (you can use Windows Explorer):
%systemroot%\Microsoft.NET\Framework

The following folders contain the released versions of the .NET Framework:
Folders with vN.N.NXXXX format, contain beta or pre-released versions.
* v3.5
* v3.0
* v2.0.50727
* v1.1.4322
* v1.0.3705

Find Mscorlib.dll inside each of the above mentioned folders, right click on it, click on Properties, click on [Details] tab and check [Product version]

To find all the version numbers, look at the following URL in [Microsoft Help and Support] web pages:
http://support.microsoft.com/default.aspx/kb/318785


Share/Bookmark

Thursday, January 22, 2009

C# - How to run a program in DOS command

The approach that comes in the following, could similarly be used to run command line tools, and DOS commands.

public bool runAcmd(string strCmd, string strCmdWorkDir)
{
// "strCmd" can contain command line arguments
bool returnVal = true;
Process prcs;
ProcessStartInfo psi = new ProcessStartInfo();

psi.FileName = "cmd";
psi.Arguments = " /c " + strCmd;
psi.WorkingDirectory = strCmdWorkDir;
psi.WindowStyle = ProcessWindowStyle.Hidden;

try
{
   prcs = Process.Start(psi);
   // wait value is in milliseconds
   if (!prcs.WaitForExit(10000))
      returnVal = false;
}
catch (Exception ex)
{
   returnVal = false;
}
finally
{
   // Some lines of code to execute in
   // case of error or not, if necessary
}
return returnVal;
}

Share/Bookmark