الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

code for circular clipping

مغلق
بدأه Ayah في 15 نوفمبر 2004 · 5 رد · 566 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Salam

Can anyone help me in devising an algorithm for clipping a line using a circular clippping region??

I wrote a code according to this algorithm

First, need a test to determine if a point lies within the circle

using the following code

static final byte LEFT_UP=1;
static final byte LEFT_DOWN=4;
static final byte RIGHT_UP=2;
static final byte RIGHT_DOWN=8;

int clipXCenter,clipYCenter; //are variables for the coordinates of the center of the circle.
float radius; //is the radius of the circle

private byte getCode(Point point)
{
byte code=0;
float r=sqrt(pow((abs(clipYCenter-point.y),2)+pow(abs(clipYCenter-point.y),2));
//r is the distance between Center of the circle to the endpoint of the line
if ((point.x<clipXCenter)&&(point.y<clipYCenter))
code||=LEFT_UP;

if ((point.x<clipXCenter)&&(point.y>clipYCenter))
code||=LEFT_DOWN;

if ((point.x>clipXCenter)&&(point.y<clipYCenter))
code||=RIGHT_UP;

if ((point.x>clipXCenter)&&(point.y>clipYCenter))
code||=RIGHT_DOWN;
return code
}

Second, if both endpoints of the line lie within the circle, the line left untouched

also here I wrote code to left the line untouched

Third, if the two endpoints lie outside the circle

HERE I FACED A PROBLEM

اقتباس
here how I can know if the line whose two endpoints lie outside the cirle have an intersection with the circle??

1+1=10

#2

thanks for you

I found the algorithm after hard thinking and this is

For simplicity, translate the centre point of the circle to the origin (you can shift the intersection points,

if any, back to the original location afterwards).

اقتباس
circle: x^2+y^2 = r^2

line: y= a*x+b

Substitute y by a*x+b in the first equation:

اقتباس
x^2+(a*x+B  )^2 = r^2

Solve this quadratic equation w.r.t. x. This gives you zero real values (the line doesn't intersect the

circle), or one real value (the line 'touches' the circle) or two values (a real intersection). If a real solution

is present, fill in x1 and x2 (possibly x1 == x2) in the second equation to obtain the corresponding

y1, y2 values. The (possibly equal) points (x1, y1) and (x2, y2) are the points where the line gets

clipped by the circle.

after I wrote the code for it I will put it in the forum

byeeee

تم تعديل هذه المشاركة بواسطة Ayah في 15 نوفمبر 2004 في 19:11

1+1=10

#3

اعتقد هيك سؤال سبق وان صادفته ليس بالصعب

لست متواجد بشكل يومي

#4
اقتباس
اعتقد هيك سؤال سبق وان صادفته ليس بالصعب

يا أخ مجدي محمود

انا بعتقد انه ما في شي بكون سهل ببدايته وهذا ينطبق على كل المبتدئين

وبعدين انا ما شفت حدا رد على الموضوع فحبيت اني اكتب الفكره بالرغم من انها سهله متل ما بتحكي

وشكرااااا الك

1+1=10

#5
اقتباس
انا بعتقد انه ما في شي بكون سهل ببدايته وهذا ينطبق على كل المبتدئين

صحيح 100 %

اقتباس
وبعدين انا ما شفت حدا رد على الموضوع فحبيت اني اكتب الفكره بالرغم من انها سهله متل ما بتحكي

مبين عصبت علي في الحقيقة رايت سؤالك في الصباح وكنت على عجلة من امري لذلك لم استطع المساعدة ودخلت خصيصا في المساء لسؤالك فوجدتك عرفت الفكرة والحمد لله وما تزعلي علينا يا اية

تم تعديل هذه المشاركة بواسطة مجدي محمود في 15 نوفمبر 2004 في 21:36

لست متواجد بشكل يومي

#6

انا ما زعلت بس الصراحه عصبت شوي وشكرا لأنك غلبت حالك ميشان ترجع تكتب الحل بس على فكره

والحمد لله طلع معي الحل

سلام

1+1=10

هذا الموضوع مغلق.

مواضيع مشابهة