Advertisement

04.24.2008 at 09:13AM PDT, ID: 23350809
[x]
Attachment Details

Zooming / Sizing in drawing App

Asked by Dodsworth in Microsoft Visual Basic.Net, Game Programming, Theory

Tags: vb.net

I have a little (but growing) drawing app that allows a collection of shape objects to be moved around the screen.

I need some pointers as to how to zoom in and out.

My shape class stores the location and size of each shape, so I guess I need to add some sort of multiplier to the drawing functions.  I also need o consider the size of the user screen

Any ideas ?Start Free Trial
 
Loading Advertisement...
 
[+][-]04.24.2008 at 09:39AM PDT, ID: 21432748

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.24.2008 at 11:53PM PDT, ID: 21437464

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.29.2008 at 03:47AM PDT, ID: 21461000

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 07:27AM PDT, ID: 21492458

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 07:35AM PDT, ID: 21492490

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 09:26AM PDT, ID: 21492822

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.10.2008 at 03:53AM PDT, ID: 21538460

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.10.2008 at 03:48PM PDT, ID: 21540816

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Game Programming, Theory
Tags: vb.net
Sign Up Now!
Solution Provided By: JoseParrot
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.24.2008 at 09:39AM PDT, ID: 21432748
Often times you can just use one of the "Transform" methods:
http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.scaletransform.aspx

After performing the scaling operation...drawing with the same coordinates will look smaller or bigger...
 
04.24.2008 at 11:53PM PDT, ID: 21437464
That kind of works in so much as my shapes are bigger but I have clip and invalidate and hittest code that nolonger works.

I really need some math on the coordinates.
 
04.29.2008 at 03:47AM PDT, ID: 21461000
Help.  I'm going stale !
 
05.03.2008 at 07:27AM PDT, ID: 21492458
Hi,

If yours is a bitmap drawing, then each zoom-in is a rescaling of image, which has bad results when magnification is large.

Assuming vector drawing for the shapes, you can define a zoomfactor and render the entire set of shapes each time you zoom or displace the viewport. A sample pseudocode follows.

In the example the orign is set in the middle of the viewport. This simplify the computation of scaling an object, in general doing by moving it to the orign, multiply its vertices by a factor and moving it back to its previous place. Assuming the orign in the center of the viewport we can simply multiply its coordinates by the desired factor.
Of course, when drawing each object you can check first if it is inside the viewport, then clipping it or even skiping it, but I think you already is taking care about that.

Jose
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
shape reactangle(P1,P2)
viewport V(V1,V2)
zoomFactor Z           // default Z=1
displacement D(dx,dy)  // defauly dx=0, dy=0
 
function render(shapes, Z, D)
{
  foreach shape
    s1.x = P1.x * Z - dx
    s1.y = P1.y * Z - dy
    s2.x = P2.x * Z - dx
    s2.y = P2.y * Z - dy
    draw shape(s1,s2)
}
Open in New Window
 
Viewport and object. Orign at center of the viewport.
Viewport and object. Orign at center of the viewport.
 
 
05.03.2008 at 07:35AM PDT, ID: 21492490
An additional comment.
If you want the center of the viewport to be static in the zooming, then the lines
    s1.x = P1.x * Z - dx
    etc.
should be replaced by
    (s1.x = P1.x  - dx) * Z
    etc.

Jose
 
05.03.2008 at 09:26AM PDT, ID: 21492822
If your program adopts orign O(0,0) at left lower or left upper then it would be a huge task to change everithing. This is bad idea.
If it is the case, then you can use the procedure described before. A sample Windows C++ code to draw a rectangle is provided below.

  (point - displacement)      moves the object to the orign,
  *Z                  scale the object
  +displacement            moves the object back

Jose
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
void DrawRectangle(HDC hdc,POINT point,HPEN pencolor, 
     int x1, int y1, int x2, int y2, // shape end points
     float Z
{
	int dx,dy; // displacement
 
	dx=Width/2; dy=Height/2; // middle size of the viewport
 
	MoveToEx(hdc,(x1-dx)*Z+dx, (y1-dy)*Z+dy, &point);
	LineTo(hdc,  (x2-dx)*Z+dx, (y1-dy)*Z+dy);
	LineTo(hdc,  (x2-dx)*Z+dx, (y2-dy)*Z+dy);
	LineTo(hdc,  (x1-dx)*Z+dx, (y2-dy)*Z+dy);
	LineTo(hdc,  (x1-dx)*Z+dx, (y1-dy)*Z+dy);
}
Open in New Window
 
05.10.2008 at 03:53AM PDT, ID: 21538460
Sorry Jose, I'm not understanding the viewport and centering concept...

"In the example the orign is set in the middle of the viewport. This simplify the computation of scaling an object, in general doing by moving it to the orign, multiply its vertices by a factor and moving it back to its previous place. Assuming the orign in the center of the viewport we can simply multiply its coordinates by the desired factor."

Can you explain a bit more
 
05.10.2008 at 03:48PM PDT, ID: 21540816
No problem, this can be little bit confuse...

When the viewport center is coincident to the orign (0,0), we can arrange the objects such that we can scale objects simply by multiplying the vertices of the objects by scale factors, say FX and FY.

When the origin is located at a viewport corner, to scale objects, we first move them to the orign, then scale them, and move them back.

The first approach avoids the two moves.

In the sample code you can notice in line 10, the expression:
   (x2-dx)*Z+dx
what means this expression?
First, we move x2 to the orign:
    tmp = x2-dx
then we scale, by multiplying by Z
    tmp = tmp * Z
and finally we move it back
   tmp = tmp + dx

If the viewport center is the orign, then we can let dx=0 and dy=0.
That way, the function
    LineTo(hdc,  (x2-dx)*Z+dx, (y1-dy)*Z+dy);
can be reduced to
    LineTo(hdc,  (x2 - 0)*Z+0, (y1 - 0)*Z+0);
that is the same as
    LineTo(hdc,  x2*Z, y1*Z);

If you are looking for a general code, which uses the window viewport (orign at upper left corner), then it is better to adopt approach 2.
At figures 1 and 2 we can watch the result after zooming in.

Jose
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Code to call the DrawRectangles function:
 
	DrawRectangles(hdc,point,penBlue,100,50, 250,135,zoom)
	DrawRectangles(hdc,point,penRed,180,97,350,258,zoom)
 
Code of DrawRectangles function
 
void DrawRectangle(HDC hdc,POINT point,HPEN pencolor, x1, y1, x2, y2, double Z)
{
	dx=Width/2; dy=Height/2; <-- equivalent to put orign at center of the viewport
 
	x1 = (int)((x1-dx+offsetx)*Z)+dx; y1 = (int)((y1-dy)*Z)+dy;
	x2 = (int)((x2-dx+offsetx)*Z)+dx; y2 = (int)((y2-dy)*Z)+dy;
	SelectObject(hdc, pencolor);
	MoveToEx(hdc,x1-1, y1, &point);
	LineTo( hdc, x2, y1);
	LineTo( hdc, x2, y2);
	LineTo( hdc, x1, y2);
	LineTo( hdc, x1, y1);
}
Open in New Window
 
Original rectangles as in the code.
Original rectangles as in the code.
 
 
Rectangles after twice zoom Z=0.1
Rectangles after twice zoom Z=0.1
 
Accepted Solution
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628