LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2004, 08:58 PM   #1
elvee
Member
 
Registered: Aug 2004
Distribution: mandrake 10
Posts: 38

Rep: Reputation: 15
Simple circles


Hey. I'm working on a destructable terrain program. Most of it is working however there is a problem that Im stumped on. How would you get the x and y of every point inside of a circle, using only the x,y of the center, and a radius?
Right now my explosions are a square (not very realistic). using this:
Code:
                int temp;
                for(temp=-radius;temp<radius;temp++)
                {
                        for(int i=0;i<radius;i++)
                                maps->destroy(x+i, y+temp);
                        for(int i=0;i<radius;i++)
                                maps->destroy(x-i, y+temp);
                }
maps->destroy(int x, int y) destroys a certain pixel of the map.

Can anyone help me turn the explosions into circles?
 
Old 09-20-2004, 09:13 PM   #2
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Well you have the general idea down as to how to destroy a circle. Now the problem is really a question of increasing how many points you destroy as you go about the circle. It is rather hard to guess what your radius field represents but i will assume it is not representing a pixel. Circles are essentially impossible to mimic in a game since you would need an infinite resolution and an infinite amount of deleted pixels to have a perfect circle. Rahter in games whe just delete enough pixels to give the illusion that there is a circle.
 
Old 09-20-2004, 09:25 PM   #3
elvee
Member
 
Registered: Aug 2004
Distribution: mandrake 10
Posts: 38

Original Poster
Rep: Reputation: 15
The radius is an actual pixel value.

And I am not concerned about an exact circle, I just want something that looks more realistic than a square explosion.
 
Old 09-20-2004, 10:02 PM   #4
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Well then you need to scrap all that code. As they say it is rather hard to make a circle with squares.

Now you really have a couple options.
1). Use a polar coordinate algorithm that uses sin/cos to draw a circle.
Something like:
dx = x + r.cos(theta)
dy = y + r.sin(theta)
where dx is the x coordinate to destroy, r is the radius and theta is the angle in radians
2). Use an algorithm such as Bresenham's to draw a circle.
x = r*cos(theta)
y = r*sin(theta)
where x is the coordinate to destroy, r is the radius and theta the angle in radians.
 
Old 09-20-2004, 11:06 PM   #5
elvee
Member
 
Registered: Aug 2004
Distribution: mandrake 10
Posts: 38

Original Poster
Rep: Reputation: 15
So it will be harder then I though. Only in grade 11, and didnt do to good in math last year
 
Old 09-21-2004, 04:21 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Just a quick idea for a simpler algorithm:

From the center point of your circle, we'll perform the destroys in 4 quarters, dividing the areas along the compass axis (eg. NSEW)

Eg bottom right quarter:
Start deleting pixels in a horizontal row from the center until you reach limit().
Then start on the next row, delete along until limit() which will be slightly less.
Then start on the next row, delete along until limit() which will have decreased greater than it did the first time.
Repeat until you're radius rows down/at the bottom of the final circle.

Limit() is a function which when given the row number and radius returns the horizontal distance from the center (eg distance along the current row) at which the edge of the circle is reached.

This can be calculated by the fact that the traced curved edge is sinusoidal/in this case follows the value of the second quarter of a sine curve, between 90 and 180 degrees. You'd just have to do a bit of division to get what fraction of the way down the radius each row is (equal to how far between the 90 and 180 limits we are), and probably use integer calculations so you're rounding to the nearest whole pixel.
 
Old 09-21-2004, 08:41 AM   #7
elvee
Member
 
Registered: Aug 2004
Distribution: mandrake 10
Posts: 38

Original Poster
Rep: Reputation: 15
Ok thanks.
 
Old 09-21-2004, 10:52 AM   #8
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Another suggestion (and it partly depends on your implementation, which sounds like a large bitmap if you are working on a per-pixel basis).

You could just generate one or more binary (or even greater) mask of whatever shape you want your explosions to look like and use that ('AND'ing with your terrain or whatever calculation you would like for more complicated effects).
 
Old 09-21-2004, 01:22 PM   #9
elvee
Member
 
Registered: Aug 2004
Distribution: mandrake 10
Posts: 38

Original Poster
Rep: Reputation: 15
Im using a raw image format (.pnm) for maps. the filesize is huge, but it can be compressed via gz or bz2 really well..

Problem with using the masks is that its harder to control the size of the explosion.

I'll consider it though. It seems to have a lot of advantages that I might want (irregular shapes specifically)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
MDK 10.2 install running me in circles micromidget Mandriva 15 03-23-2005 08:44 PM
problems creating a simple bootable cd to perform a simple task czarherr Linux - Software 1 11-11-2004 05:22 AM
mounting usb thumbdrive and going in circles slackist Linux - Hardware 11 07-27-2004 08:30 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration