How to detect the Left-Top Corner of a Rectangle around anObject?

when it comes to programming, with almost everything the rule is if you can
think it, you can do it (:

is it more trouble than it’s worth though? that remains to be seen :stuck_out_tongue:

1 way would be lets assume the object is a certain color and the background
is a different color, or atleast that you know how to tell by the color, if
any given pixel is background or object (ie picture is black and white…or
background is black and the object can be any other color, or any other rule
which seperates background colors from foreground colors)

what you do then would be something like this (pseudo code)…

minx=miny=100000;
maxx=maxy=0;

for x=0 to width of surface
for y = 0 to height of surface
{
color = get pixel color at x,y on the surface;
if pixel color means it’s a part of the object
{
if xmaxx
maxx=x;
if ymaxy
maxy=y;
}
}

at the end of this loop, (minx,miny) - (maxx,maxy) will describe the
smallest needed rectangle to store the object.

if this isnt a viable solution for your problem give us some more details
and we might be able to come up with a more appropriate solution.> ----- Original Message -----

From: phuoccanhua@yahoo.com.au (Phuoc Can Hua)
To:
Sent: Tuesday, August 31, 2004 8:56 PM
Subject: [SDL] How to detect the Left-Top Corner of a Rectangle around
anObject?

If I have an object on a surface and I want to find
out the left-top corner of the minimal rectangle
around it (i.e. the box crops the transparent part)
can I do that?

thanks
Phuoc Can HUA

=====
/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle

Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan,
Your solution is absolutely right, thanks. I was only
hoping that SDL might have some functions for that
already :frowning: because SDL might maintain the
information somewhere whenever a pixel is drawn.

Does SDL_surface has an alpha channel? Is the surface
initialized as tranparent? If not, I always have to
make sure the background color does not appear at the
edge of the object.

Phuoc Can HUA

— Alan Wolfe wrote:> when it comes to programming, with almost everything

the rule is if you can
think it, you can do it (:

is it more trouble than it’s worth though? that
remains to be seen :stuck_out_tongue:

1 way would be lets assume the object is a certain
color and the background
is a different color, or atleast that you know how
to tell by the color, if
any given pixel is background or object (ie picture
is black and white…or
background is black and the object can be any other
color, or any other rule
which seperates background colors from foreground
colors)

what you do then would be something like this
(pseudo code)…

minx=miny=100000;
maxx=maxy=0;

for x=0 to width of surface
for y = 0 to height of surface
{
color = get pixel color at x,y on the surface;
if pixel color means it’s a part of the object
{
if xmaxx
maxx=x;
if ymaxy
maxy=y;
}
}

at the end of this loop, (minx,miny) - (maxx,maxy)
will describe the
smallest needed rectangle to store the object.

if this isnt a viable solution for your problem give
us some more details
and we might be able to come up with a more
appropriate solution.

=====
/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle

Find local movie times and trailers on Yahoo! Movies.

Phuoc,
SDL has a function for alphas, but if all you want is a transparent area
around a bitmap, you might look into SDL_SetColorKey. For semi-transparent
SDL_SetAlpha.

AustinOn Wednesday 01 September 2004 12:39 am, Phuoc Can Hua wrote:

Alan,
Your solution is absolutely right, thanks. I was only
hoping that SDL might have some functions for that
already :frowning: because SDL might maintain the
information somewhere whenever a pixel is drawn.

Does SDL_surface has an alpha channel? Is the surface
initialized as tranparent? If not, I always have to
make sure the background color does not appear at the
edge of the object.

Phuoc Can HUA

— Alan Wolfe wrote:

when it comes to programming, with almost everything
the rule is if you can
think it, you can do it (:

is it more trouble than it’s worth though? that
remains to be seen :stuck_out_tongue:

1 way would be lets assume the object is a certain
color and the background
is a different color, or atleast that you know how
to tell by the color, if
any given pixel is background or object (ie picture
is black and white…or
background is black and the object can be any other
color, or any other rule
which seperates background colors from foreground
colors)

what you do then would be something like this
(pseudo code)…

minx=miny=100000;
maxx=maxy=0;

for x=0 to width of surface
for y = 0 to height of surface
{
color = get pixel color at x,y on the surface;
if pixel color means it’s a part of the object
{
if xmaxx
maxx=x;
if ymaxy
maxy=y;
}
}

at the end of this loop, (minx,miny) - (maxx,maxy)
will describe the
smallest needed rectangle to store the object.

if this isnt a viable solution for your problem give
us some more details
and we might be able to come up with a more
appropriate solution.

=====
/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle

Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan,
Your solution is absolutely right, thanks. I was only
hoping that SDL might have some functions for that
already :frowning: because SDL might maintain the
information somewhere whenever a pixel is drawn.

As a general rule you can find all the SDL functions in the SDL
documentation.

Does SDL_surface has an alpha channel? Is the surface
initialized as tranparent? If not, I always have to
make sure the background color does not appear at the
edge of the object.

An SDL surface may, or may not, have an alpha channel. It depends on
whether you created the surface with an alpha channel or not. It is
pretty much up to you to clear the surface, including the alpha channel,
to what ever value you want.

	Bob PendletonOn Tue, 2004-08-31 at 23:39, Phuoc Can Hua wrote:

Phuoc Can HUA

— Alan Wolfe wrote:

when it comes to programming, with almost everything
the rule is if you can
think it, you can do it (:

is it more trouble than it’s worth though? that
remains to be seen :stuck_out_tongue:

1 way would be lets assume the object is a certain
color and the background
is a different color, or atleast that you know how
to tell by the color, if
any given pixel is background or object (ie picture
is black and white…or
background is black and the object can be any other
color, or any other rule
which seperates background colors from foreground
colors)

what you do then would be something like this
(pseudo code)…

minx=miny=100000;
maxx=maxy=0;

for x=0 to width of surface
for y = 0 to height of surface
{
color = get pixel color at x,y on the surface;
if pixel color means it’s a part of the object
{
if xmaxx
maxx=x;
if ymaxy
maxy=y;
}
}

at the end of this loop, (minx,miny) - (maxx,maxy)
will describe the
smallest needed rectangle to store the object.

if this isnt a viable solution for your problem give
us some more details
and we might be able to come up with a more
appropriate solution.

=====
/_/_/_/_/_/_
www.huaonline.com
My Homepage is my Castle

Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+