473,545 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Math with coordinates

132 New Member
Hi all,

I've got a bit of a challenge. I've got a script which displays the mouse coodinates if you click on an image. Now I would like to convert these coordinates to pixelnumber. Let me see if I can make it easier. Suppose we have an image which is 100px high and 100px wide.
The coordinates of the upper left corner would be x=0 and y=0
The coordinates of the upper right corner would be x=99 and y=0
The coordinates of the lower left corner would be x=0 and y=99
The coordinates of the upper left corner would be x=99 and y=99

Now I would like to display these coordinates on an other way, by the pixel they represent. So that would mean:
The pixelnumber of the upper left corner would be 1
The pixelnumber of the upper right corner would be 100
The pixelnumber of the lower left corner would be 9901
The pixelnumber of the upper left corner would be 10000

So by using the coordinates I'm sure that these pixelnumbers could be calculated. However, I'm not capable of writing this myself. Can anyone help me with this?

Here is code that I have so far:

[HTML]<html>
<head>
<script language="JavaS cript">

function point_it(event) {
var pointer_div = document.getEle mentById("point er_div");
//IE
if (window.ActiveX Object) {
pos_x = window.event.of fsetX;
pos_y = window.event.of fsetY;
}
//Firefox >>> still to be checked!!
else {
var top = 0, left = 0;
var elm = pointer_div;
while (elm) {
left = elm.offsetLeft;
top = elm.offsetTop;
elm = elm.offsetParen t;
}

pos_x = event.pageX - left;
pos_y = event.pageY - top;
}

document.pointf orm.form_x.valu e = pos_x;
document.pointf orm.form_y.valu e = pos_y;
document.pointf orm.form_pixel. value = pixel;
}


</script>
</head>
<body>
<form name="pointform " method="post">
<div id="pointer_div " onclick="point_ it(event)" style = "background-image:url('http ://www.nasa.gov/externalflash/NASA45/27/27image.jpg');w idth:516px;heig ht:387px; CURSOR: crosshair"></div>
Coordinates<br>
x = <input type="text" name="form_x" size="4">
y = <input type="text" name="form_y" size="4">
<br>
pixel = <input type="text" name="form_pixe l" size="8">
</form>
</body>
</html>[/HTML]
Nov 25 '07 #1
1 2296
Cainnech
132 New Member
Hi guys,

It's me again. I thought I would never figure out how to do it. Until I realised that I was looking at it from the wrong point of view.

I just needed to find the formula to calculate the pixels. So I started out in Excel trying to find the right formula. Once I found that one, I just had to convert it into javascript. And what do you know it actually works!

So I doubt if anyone would really need such a script but anyway, here it is:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4.  
  5. function point_it(event){
  6. var pointer_div = document.getElementById("pointer_div");
  7. var pixel;
  8. var imagewidth="516";
  9. var calc;
  10. var top = 0, left = 0;
  11. var elm = pointer_div;
  12.  
  13. //IE
  14. if (window.ActiveXObject) {
  15. pos_x = window.event.offsetX;
  16. pos_y = window.event.offsetY;
  17. }
  18.  
  19. //Firefox >>> still to be checked!!
  20. else {
  21. while (elm) {
  22. left = elm.offsetLeft;
  23. top = elm.offsetTop;
  24. elm = elm.offsetParent;
  25. }
  26. pos_x = event.pageX - left;
  27. pos_y = event.pageY - top;
  28. }
  29.  
  30. if (pos_y <= "0"){
  31. calc = (pos_y)
  32. }
  33. else{
  34. calc = (parseInt(pos_y)*parseInt(imagewidth) - parseInt(pos_y))
  35. }
  36.  
  37. pixel = (parseInt(pos_y) + parseInt(pos_x+1)) + calc;
  38.  
  39.  
  40.  
  41. // Debugging
  42. document.pointform.form_x.value = pos_x + 1;
  43. document.pointform.form_y.value = pos_y + 1;
  44. document.pointform.form_pixel.value = pixel;
  45. }
  46.  
  47. </script>
  48. </head>
  49. <body>
  50. <form name="pointform" method="post">
  51. <div id="pointer_div" onclick="point_it(event)" style = "background-image:url('http://www.nasa.gov/externalflash/NASA45/27/27image.jpg');width:516px;height:387px; CURSOR: crosshair"></div>
  52.  
  53. <!-- Debugging -->
  54. Coordinates<br>
  55. x = <input type="text" name="form_x" size="4">
  56. y = <input type="text" name="form_y" size="4">
  57. <br>
  58. pixel = <input type="text" name="form_pixel" size="8">
  59. </form> 
  60. </body>
  61. </html> 
  62.  
Good luck to all!
Nov 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2904
by: Bill Stockwell | last post by:
I have upgraded PHP on our server (a RedHat 7.2 box) to version 4.3.5. However, now code I wrote in PHP before fails to work correctly. I have some code that invokes the math function atan2 in connection with converting rectangular to polar coordinates in a Web page. This used to work in PHP 4.x (earlier version) but failed with 4.3.4 and...
0
2206
by: Suresh Kumar | last post by:
Hi, In Tkinter, how to convert canvas coordinates to window coordinates? To convert window coordinates to canvas coordinates we are using "canvasx,canvasy". But how to do vice-versa? Iam using a canvas whose size is larger than screen size and drawn a rectagnle in the middle of canvas. When i select the rectangle i got its coordinates in...
2
13874
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all have a score for quality and are sorted on this score. What I want to do is to sort these coordinates first on x and define groups (regions) of...
17
3595
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time...
6
1649
by: Michael Trausch | last post by:
Hello everyone, I'm having a little bit of trouble trying to implement some arithmetic logic into an application that I'm working on, and I'm hoping that somebody can possibly point me in the right direction. I am working with a database with ZIP codes, latitudes, and longitudes, and am working to implement the Haversine formula alongside...
3
5377
by: steve | last post by:
Hi All I have textboxes within a TableLayoutpanel and I want to be able to position an independant control adjacent to a selected textbox This independent control allows selection of text to insert into the textbox I am having trouble achieving this, see code below, the x position is too far to the right and the y position is close to...
7
2231
by: shaun roe | last post by:
Hi, I am using an XSLT to generate an SVG client-side in Firefox. The user opens an XML file in Firefox and sees a display. In doing so, I have to convert from cartesian to polar coordinates, so I need the sine and cosine functions. Firefox does not seem to recognize the math: extensions (not too surprising) so I am using a series...
2
3713
geo17
by: geo17 | last post by:
I am new to using Access 2007 and I am creating a database for work. On one form I have text boxes for GPS coordinates. One set is a known point in x and y and the other set is a measured x and y (UTM coordinates). I want to display the offset of these two different measurements. Simple equation. I just don't know what is the best way to do this...
0
8913
by: raylopez99 | last post by:
keywords: logical coordinates, page coordinates, world coordinates, device coordinates, physical coordinates, screen coordinates, client coordinates. offset rectangle. WYSIWYG rubber rectangle problem, bounded rectangle problem. PointToClient, PointToScreen Beware this newbie trap for the unwary. It goes by various names (in some old...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.