1. 程式人生 > >代寫C語言程序設計作業、代作國外PickNearestNod C作業

代寫C語言程序設計作業、代作國外PickNearestNod C作業

RM rect splay win timers display 圓角 相等 SM


#include "graphics.h"
#include "extgraph.h"
#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include "strlib.h"
#include "conio.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stddef.h>

#include <windows.h>
#include <olectl.h>
#include <mmsystem.h>
#include <wingdi.h>
#include <ole2.h>
#include <ocidl.h>
#include <winuser.h>

#include "linkedlist.h"

/*changed*/
#define NLIST 7
#define LINE 0
#define ARROWLINE 1
#define RECT 2
#define ROUNDEDRECT 3
#define DIAMOND 4
#define ELLIPSE 5
#define STRING 6

#define SYSCOLOR "Red" /*默認繪圖顏色*/

#define CURSOR "_" /*光標符號*/
#define CURSOR_BLINK 1 /*光標閃爍定時器事件標誌號*/
const int timerseconds = 500; /*光標閃爍周期ms*/
bool isCursorBlink = FALSE; /*光標是否閃爍*/

void KeyboardEventProcess(int key,int event);/*鍵盤消息回調函數*/
void CharEventProcess(char c);/*字符消息回調函數*/
void MouseEventProcess(int x, int y, int button, int event);/*鼠標消息回調函數*/
void TimerEventProcess(int timerID);/*定時器消息回調函數*/

typedef struct {/*直線類型*/
double x1, y1;/*第一個點坐標*/
double x2, y2;/*第二個點坐標*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} *LineT;

/*added*/
typedef struct {/*帶箭頭直線類型*/
double x1, y1;/*第一個點坐標*/
double x2, y2;/*第二個點坐標*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} *ArrowLineT;

typedef struct {/*矩形類型*/
double x1, y1;/*左下角坐標*/
double x2, y2;/*右上角坐標*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} *RectT;

/*added*/
typedef struct {/*圓角矩形類型*/
double x1, y1;/*左下角坐標*/
double x2, y2;/*右上角坐標*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} *RoundedRectT;

/*added*/
typedef struct {/*菱形類型*/
double x1, y1;/*左下角坐標*/
double x2, y2;/*右上角坐標*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} * DiamondT;

typedef struct {/*橢圓類型*/
double cx, cy;/*圓心*/
double rx, ry;/*長短半徑*/
int PenSize; /*粗細*/
string color; /*顏色*/
bool isSelected; /*選中*/
} *EllipseT;

typedef struct {/*文本類型*/
string text;/*文本指針*/
double x, y;/*文本顯示起始位置坐標*/
int PointSize; /*文字大小*/
string color; /*顏色*/
bool isSelected; /*選中*/
int curPos; /*光標位置*/
bool isDisplayed; /*光標是否處於顯示狀態*/
} *TextT;

/*changed*/
linkedlistADT list[NLIST]={NULL, NULL, NULL, NULL, NULL, NULL, NULL};/*七類圖形元素鏈表指針*/
double minDistance[NLIST]={100000000.0, 1000000000.0, 1000000000.0, 1000000000.0, 1000000000.0, 1000000000.0, 1000000000.0};
int curList = LINE; /*當前鏈表序號*/
LineT curLine = NULL;/*直線鏈表的當前對象指針*/
ArrowLineT curArrowLine = NULL;/*帶箭頭直線鏈表的當前對象指針*/
RectT curRect = NULL;/*矩形鏈表的當前對象指針*/
RoundedRectT curRoundedRect = NULL;/*圓角矩形鏈表的當前對象指針*/
DiamondT curDiamond = NULL;/*菱形鏈表的當前對象指針*/
EllipseT curEllipse = NULL;/*橢圓鏈表的當前對象指針*/
TextT curText = NULL;/*文本鏈表的當前對象指針*/

bool isSelected = FALSE; /*圖形元素選中狀態*/

#define TEXTLEN 100
static TextT tptr; /*當前編輯文本對象指針*/
static bool inText = FALSE; /*是否處於當前文本編輯狀態*/
static char textbuf[TEXTLEN+1];/*當前文本緩沖區*/

/*橢圓處理函數*/
void DrawCenteredEllipse(void *ellipse);/*畫橢圓*/
bool EllipseEqual(void *ellipse1, void *ellipse2);/*比較兩個橢圓相等*/
EllipseT SelectNearestNodeE(linkedlistADT list, double mx, double my);/*選擇靠(mx,my)最近的結點*/

/*直線處理函數*/
void DrawLineD(void *line);
bool LineEqual(void *line1, void *line2);
LineT SelectNearestNodeL(linkedlistADT list, double mx, double my);

/*added*/
/*帶箭頭直線處理函數*/
void DrawArrowLineD(void *arrowline);
bool ArrowLineEqual(void *arrowline1, void *arrowline2);
ArrowLineT SelectNearestNodeAL(linkedlistADT list, double mx, double my);

/*矩形處理函數*/
void DrawRect(void *rect);
bool RectEqual(void *rect1, void *rect2);
RectT SelectNearestNodeR(linkedlistADT list, double mx, double my);

/*added*/
/*圓角矩形處理函數*/
void DrawRoundedRect(void *roundedrect);
bool RoundedRectEqual(void *roundedrect1, void *roundedrect2);
RoundedRectT SelectNearestNodeRR(linkedlistADT list, double mx, double my);

/*added*/
/*菱形處理函數*/
void DrawDiamond (void *diamond);
bool DiamondEqual(void * diamond1, void * diamond2);
DiamondT SelectNearestNodeD(linkedlistADT list, double mx, double my);

/*文本處理函數*/
void DrawTextT(void *text); /*顯示文本*/
bool TextEqual(void *text1, void *text2);/*比較兩個文本是否相同*/
TextT SelectNearestNodeT(linkedlistADT list, double mx, double my);
void InsertCharToString(string str, int pos, char c);/*將字符c插入到字符串str的pos位置*/
void DeleteCharFromString(string str, int pos);/*刪除字符串str的pos位置字符*/
void DrawCursor(string str, int curPos, double startx, double starty);/*顯示文本光標*/

/*added*/
void init();/*初始化函數*/
void PickNearestNode(linkedlistADT list[], double mx, double my);/*選擇靠(mx,my)最近結點*/
void TraverseAllList();/*刷新所有圖形對象*/

void Main() /*僅初始化執行一次*/
{
int i;

InitGraphics();/*圖形初始化*/
Randomize();/*隨機函數初始化*/
registerKeyboardEvent(KeyboardEventProcess);/*註冊鍵盤消息回調函數*/
registerCharEvent(CharEventProcess);/*註冊字符消息回調函數*/
registerMouseEvent(MouseEventProcess);/*註冊鼠標消息回調函數*/
registerTimerEvent(TimerEventProcess);/*註冊定時器消息回調函數*/

SetPenColor(SYSCOLOR);
SetPenSize(1);

for (i = 0; i < NLIST; i++) list[i] = NewLinkedList();

/*added*/
init();
}

/*added*/
void init()
{
double totalY=GetWindowHeight();
double distanceY=totalY*3/37;
double distanceX=totalY*9/148;
double dy=totalY*4/37;
double dx=totalY*16/111;
LineT lptr;
ArrowLineT alptr;
RectT rptr;
RoundedRectT rrptr;
DiamondT dptr;
EllipseT eptr;

/*line init*/
lptr = GetBlock(sizeof(*lptr));
lptr->x1 = distanceX;
lptr->y1 = 6*distanceY+4*dy;
lptr->x2 = distanceX+dx;
lptr->y2 = 6*distanceY+4*dy;
lptr->PenSize = GetPenSize();
lptr->color = GetPenColor();
lptr->isSelected = FALSE;
InsertNode(list[LINE], NULL, lptr);
DrawLineD(lptr);

/*arrowLine init*/
alptr = GetBlock(sizeof(*alptr));
alptr->x1 = distanceX;
alptr->y1 = 5*distanceY+4*dy;
alptr->x2 = distanceX+dx;
alptr->y2 = 5*distanceY+4*dy;
alptr->PenSize = GetPenSize();
alptr->color = GetPenColor();
alptr->isSelected = FALSE;
InsertNode(list[LINE], NULL, alptr);
DrawArrowLineD(alptr);

/*rectangle init*/
rptr = GetBlock(sizeof(*rptr));
rptr->x1 = distanceX;
rptr->y1 = 4*distanceY+3*dy;
rptr->x2 = distanceX+dx;
rptr->y2 = 4*distanceY+4*dy;
rptr->PenSize = GetPenSize();
rptr->color = GetPenColor();
rptr->isSelected = FALSE;
InsertNode(list[RECT], NULL, rptr);
DrawRect(rptr);

/*roundedRectangle init*/
rrptr = GetBlock(sizeof(*rrptr));
rrptr->x1 = distanceX;
rrptr->y1 = 3*distanceY+2*dy;
rrptr->x2 = distanceX+dx;
rrptr->y2 = 3*distanceY+3*dy;
rrptr->PenSize = GetPenSize();
rrptr->color = GetPenColor();
rrptr->isSelected = FALSE;
InsertNode(list[RECT], NULL, rrptr);
DrawRect(rrptr);

/*diamond init*/
dptr = GetBlock(sizeof(*dptr));
dptr->x1 = distanceX;
dptr->y1 = 2*distanceY+1*dy;
dptr->x2 = distanceX+dx;
dptr->y2 = 2*distanceY+2*dy;
dptr->PenSize = GetPenSize();
dptr->color = GetPenColor();
dptr->isSelected = FALSE;
InsertNode(list[RECT], NULL, dptr);
DrawRect(dptr);

/*ellipse init*/
eptr = GetBlock(sizeof(*eptr));
eptr->cx = distanceX+0.5*dx;
eptr->cy = distanceY+0.5*dy;
eptr->rx = 0.5*dx;
eptr->ry = 0.5*dy;
eptr->PenSize = GetPenSize();
eptr->color = GetPenColor();
eptr->isSelected = FALSE;
InsertNode(list[ELLIPSE], NULL, eptr);
DrawCenteredEllipse(eptr);
}

void KeyboardEventProcess(int key,int event)/*每當產生鍵盤消息,都要執行*/
{
/*deleted*/

switch (event) {
case KEY_DOWN:
switch (key) {
/*deleted*/
/*useful!!!!!!!!!*/
case VK_F4:
if (inText) break;/*若已處於文本編輯狀態,則不生成新的空文本*/
inText = TRUE; /*進入新的文本行輸入狀態*/
tptr = GetBlock(sizeof(*tptr));
tptr->x = RandomReal(1.0, GetWindowWidth()-1.0);
tptr->y = RandomReal(1.0, GetWindowHeight()-1.0);
MovePen(tptr->x, tptr->y);/*文本顯示起始位置*/
DrawTextString(CURSOR);/*顯示光標*/
textbuf[0] = ‘\0‘; /*形成字符串*/
tptr->PointSize = GetPointSize();
tptr->color = GetPenColor();
tptr->isSelected = FALSE;
tptr->curPos = 0; /*設置當前光標位置*/
tptr->isDisplayed = TRUE; /*設置光標閃爍標誌*/
startTimer(CURSOR_BLINK, timerseconds);/*光標閃爍定時器觸發*/
isCursorBlink = TRUE;/*設置光標閃爍標誌*/
break;

case VK_F5: /*編輯已有文本行*/
if (!isSelected || curList != STRING) break;/*當前不處於文本行編輯狀態*/
if (inText) break;/*已處於文本行編輯狀態*/
inText = TRUE;/*設置文本編輯狀態*/
tptr = curText;/*取當前選中的文本對象*/
strcpy(textbuf, tptr->text);/*將當前文本字符串拷貝到臨時文本緩沖區以備編輯*/
DeleteNode(list[STRING], curText, TextEqual);/*從當前文本鏈表中刪除該文本對象*/
SetPenColor(tptr->color); /*設置選擇文本的顏色*/
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*顯示當前文本光標*/
tptr->isDisplayed = TRUE; /*設置當前文本光標閃爍標誌*/
startTimer(CURSOR_BLINK, timerseconds);/*光標閃爍定時器觸發*/
isCursorBlink = TRUE; /*設置光標閃爍狀態*/
break;
case VK_LEFT: /*左移鍵*/
if (!inText) break;/*不處於文本編輯狀態*/
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);/*擦除當前文本*/
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*擦除光標*/
if (tptr->curPos>0) tptr->curPos--;/*左移光標一個字符位置*/
SetEraseMode(FALSE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);/*重新顯示當前文本*/
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*顯示光標*/
break;
case VK_RIGHT: /*右移鍵*/
if (!inText) break;/*不處於文本編輯狀態*/
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);/*擦除當前文本*/
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*擦除光標*/
if (tptr->curPos<strlen(textbuf)) tptr->curPos++;/*右移光標一個字符位置*/
SetEraseMode(FALSE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);/*重新顯示當前文本*/
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y); /*顯示光標*/
break;
case VK_DELETE:/*DELETE*/
if (inText) { /*若處於文本編輯狀態,則刪除當前字符*/
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除當前光標*/
DeleteCharFromString(textbuf, tptr->curPos);/*刪除當前位置的字符*/
SetEraseMode(FALSE);
TraverseAllList();/*刷新所有圖形元素*/
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*顯示當前光標*/
break;
}
if (!isSelected) break;
/*下面刪除選中的圖形元素*/
SetEraseMode(TRUE);
TraverseAllList();
switch (curList) {
case LINE:
DeleteNode(list[LINE], curLine, LineEqual);
break;
case RECT:
DeleteNode(list[RECT], curRect, RectEqual);
break;
case ELLIPSE:
DeleteNode(list[ELLIPSE], curEllipse, EllipseEqual);
break;
case STRING:
DeleteNode(list[STRING], curText, TextEqual);
break;
}
SetEraseMode(FALSE);
TraverseAllList();
isSelected = FALSE;
break;
case VK_ESCAPE:/*退出圖形元素選中狀態*/
if (!isSelected) break;/*不處於選中狀態則不處理*/
switch (curList) {
case LINE:
curLine->isSelected = FALSE;
curLine->color = GetPenColor();
break;
case RECT:
curRect->isSelected = FALSE;
curRect->color = GetPenColor();
break;
case ELLIPSE:
curEllipse->isSelected = FALSE;
curEllipse->color = GetPenColor();
break;
case STRING:
curText->isSelected = FALSE;
curText->color = GetPenColor();
break;
}
isSelected = FALSE;
TraverseAllList();
break;
}
break;
case KEY_UP:
break;
}
}

void CharEventProcess(char c)
{
int len;

if (!inText) return;
switch (c) {
case 27: /*ESC*/
case ‘\r‘: /* 註意:回車在這裏返回的字符是‘\r‘,不是‘\n‘*/
inText = FALSE;/*退出當前文本輸入*/
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除當前光標*/
TraverseAllList();
tptr->isDisplayed = FALSE;
tptr->text = CopyString(textbuf);/*申請字符串空間*/
InsertNode(list[STRING], NULL, tptr);
SetEraseMode(FALSE);
TraverseAllList();
cancelTimer(CURSOR_BLINK);/*註銷光標閃爍定時器*/
isCursorBlink = FALSE;
SetPenColor(SYSCOLOR);
break;
case ‘\b‘:/*BACKSPACE*/
if ((len = strlen(textbuf)) == 0) break;
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除當前光標*/
DeleteCharFromString(textbuf, tptr->curPos-1);
SetEraseMode(FALSE);
TraverseAllList();
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
if (tptr->curPos > 0) tptr->curPos--;
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*顯示當前光標*/
break;
default:
if ((len = strlen(textbuf)) >= TEXTLEN) break;
SetEraseMode(TRUE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*擦除當前光標*/
InsertCharToString(textbuf, tptr->curPos, c);/*將當前字符插入到光標位置*/
SetEraseMode(FALSE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
tptr->curPos++;
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);/*顯示當前光標*/
break;
}
}

void MouseEventProcess(int x, int y, int button, int event)
{
static bool isMove = FALSE; /*移動標誌*/
static bool isChangeSize = FALSE; /*縮放標誌*/
static double omx = 0.0, omy = 0.0;/*前一鼠標坐標*/
double mx, my;/*當前鼠標坐標*/
double x1, y1, x2, y2, dx, dy;

mx = ScaleXInches(x);/*pixels --> inches*/
my = ScaleYInches(y);/*pixels --> inches*/

switch (event) {
case BUTTON_DOWN:
if (isSelected) { /*已是選中狀態*/
omx = mx;
omy = my;
if (button == LEFT_BUTTON) { /*左鍵按下,進入移動狀態*/
isMove = TRUE;
} else if (button == RIGHT_BUTTON) {/*右鍵鍵按下,進入縮放狀態*/
isChangeSize = TRUE;
}
break;
}
PickNearestNode(list, mx, my);/*選擇對象*/
switch (curList) {
case LINE:
curLine->isSelected = TRUE;
curLine->color = CopyString("GREEN");
break;
/*added*/
case ARROWLINE:
curARROWLINE->isSelected = TRUE;
curARROWLINE->color = CopyString("GREEN");
break;
case RECT:
curRect->isSelected = TRUE;
curRect->color = CopyString("GREEN");
break;
/*added*/
case ROUNDEDRECT:
curROUNDEDRECT->isSelected = TRUE;
curROUNDEDRECT->color = CopyString("GREEN");
break;
/*added*/
case DIAMOND:
curDIAMOND->isSelected = TRUE;
curDIAMOND->color = CopyString("GREEN");
break;
case ELLIPSE:
curEllipse->isSelected = TRUE;
curEllipse->color = CopyString("GREEN");
break;
case STRING:
curText->isSelected = TRUE;
curText->color = CopyString("GREEN");
break;
}
isSelected = TRUE;
TraverseAllList();
break;
case BUTTON_DOUBLECLICK:/*needed to be changed*/
break;
case BUTTON_UP:
isMove = FALSE; /*退出移動狀態*/
isChangeSize = FALSE;/*退出縮放狀態*/
break;
case MOUSEMOVE:
if (isSelected && (isMove || isChangeSize)) { /*選中狀態下,移動或縮放*/
SetEraseMode(TRUE);
TraverseAllList();
dx = mx - omx;
dy = my - omy;
if (isMove) { /*移動*//*needed to be changed*/
switch (curList){
case LINE:
curLine->x1 += dx;
curLine->y1 += dy;
curLine->x2 += dx;
curLine->y2 += dy;
break;
case RECT:
x1 = curRect->x1 + dx;
y1 = curRect->y1 + dy;
x2 = curRect->x2 + dx;
y2 = curRect->y2 + dy;
if (x1 < x2) {
curRect->x1 = x1;
curRect->x2 = x2;
} else {
curRect->x1 = x2;
curRect->x2 = x1;
}
if (y1 < y2) {
curRect->y1 = y1;
curRect->y2 = y2;
} else {
curRect->y1 = y2;
curRect->y2 = y1;
}
break;
case ELLIPSE:
curEllipse->cx += dx;
curEllipse->cy += dy;
break;
case STRING:
curText->x += dx;
curText->y += dy;
break;
}
} else if (isChangeSize) {/*縮放*/
switch (curList){
case LINE:
if (fabs(omx-curLine->x1)+fabs(omy-curLine->y1) <
fabs(omx-curLine->x2)+fabs(omy-curLine->y2)) {
curLine->x1 += dx;
curLine->y1 += dy;
} else {
curLine->x2 += dx;
curLine->y2 += dy;
}
break;
case RECT:
if (fabs(omx-curRect->x1) < fabs(omx-curRect->x2)) {
curRect->x1 += dx;
} else {
curRect->x2 += dx;
}
if (fabs(omy-curRect->y1) < fabs(omy-curRect->y2)) {
curRect->y1 += dy;
} else {
curRect->y2 += dy;
}
break;
case ELLIPSE:
curEllipse->rx += dx;
curEllipse->ry += dy;
break;
case STRING:
//curText->PointSize + (dx>0?1:-1);
break;
}
}
omx = mx;
omy = my;
SetEraseMode(FALSE);
TraverseAllList();
}
break;
}
}

void TimerEventProcess(int timerID)
{
switch (timerID) {
case CURSOR_BLINK: /*100ms光標閃爍定時器*/
SetEraseMode(!tptr->isDisplayed);
DrawCursor(textbuf, tptr->curPos, tptr->x, tptr->y);
SetEraseMode(FALSE);
MovePen(tptr->x, tptr->y);
DrawTextString(textbuf);
tptr->isDisplayed=!tptr->isDisplayed;
break;
default:
break;
}
}

/*for Line*/
void DrawLineD(void *line)
{
LineT lptr = (LineT)line;
int pensize = GetPenSize();/*保存當前系統筆畫粗細*/
string color = GetPenColor();/*保存當前系統顏色*/

SetPenSize(lptr->PenSize);/*設置粗細*/
SetPenColor(lptr->color);/*設置顏色*/
MovePen(lptr->x1, lptr->y1);
DrawLine(lptr->x2-lptr->x1,lptr->y2-lptr->y1);
SetPenSize(pensize); /*恢復粗細*/
SetPenColor(color);/*恢復顏色*/
}

bool LineEqual(void *line1, void *line2)
{
LineT lptr1 = (LineT)line1, lptr2 = (LineT)line2;

return lptr1->x1 == lptr2->x1 && lptr1->y1 == lptr2->y1 &&
lptr1->x2 == lptr2->x2 && lptr1->y2 == lptr2->y2;
}

double distLine(double x, double y, LineT line)
{
double x0, y0;

x0 = (line->x1 + line->x2)/2;
y0 = (line->y1 + line->y2)/2;
return fabs(x-x0) + fabs(y-y0);
}

LineT SelectNearestNodeL(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distLine(mx, my, (LineT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distLine(mx, my, (LineT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[LINE] = mindistance;

return (LineT)NodeObj(list, nearestnode);
}

/*added*/
/*for arrowLine*/
void DrawArrowLineD(void *arrowline)
{
LineT alptr = (ArrowLineT)arrowline;
int pensize = GetPenSize();/*保存當前系統筆畫粗細*/
string color = GetPenColor();/*保存當前系統顏色*/

SetPenSize(alptr->PenSize);/*設置粗細*/
SetPenColor(alptr->color);/*設置顏色*/
MovePen(alptr->x1, alptr->y1);
DrawLine(alptr->x2-alptr->x1,alptr->y2-alptr->y1);
SetPenSize(pensize); /*恢復粗細*/
SetPenColor(color);/*恢復顏色*/
/*something to do*/
}

bool ArrowLineEqual(void *arrowline1, void *arrowline2)
{
ArrowLineT alptr1 = (ArrowLineT)arrowline1, alptr2 = (ArrowLineT)arrowline2;

return alptr1->x1 == alptr2->x1 && alptr1->y1 == alptr2->y1 &&
alptr1->x2 == alptr2->x2 && alptr1->y2 == alptr2->y2;
}

double distArrowLine(double x, double y, ArrowLineT arrowline)
{
double x0, y0;

x0 = (arrowline->x1 + arrowline->x2)/2;
y0 = (arrowline->y1 + arrowline->y2)/2;
return fabs(x-x0) + fabs(y-y0);
}

ArrowLineT SelectNearestNodeAL(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distArrowLine(mx, my, (ArrowLineT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distArrowLine(mx, my, (ArrowLineT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[ARROWLINE] = mindistance;

return (ArrowLineT)NodeObj(list, nearestnode);
}

/*for rectangle */
void DrawRect(void *rect)
{
RectT r = (RectT)rect;
int pensize = GetPenSize();/*保存當前系統筆畫粗細*/
string color = GetPenColor();/*保存當前系統顏色*/

SetPenSize(r->PenSize);/*設置粗細*/
SetPenColor(r->color);/*設置顏色*/
MovePen(r->x1, r->y1);
DrawLine(r->x2-r->x1, 0);
DrawLine(0, r->y2-r->y1);
DrawLine(r->x1-r->x2, 0);
DrawLine(0, r->y1-r->y2);
SetPenSize(pensize); /*恢復粗細*/
SetPenColor(color);/*恢復顏色*/
}

bool RectEqual(void *rect1, void *rect2)
{
RectT r1, r2;

r1 = (RectT)rect1;
r2 = (RectT)rect2;
return r1->x1 == r2->x1 && r1->y1 == r2->y1 &&
r1->x2 == r2->x2 && r1->y2 == r2->y2;
}

double distRect(double x, double y, RectT rect)
{
double x0, y0;

x0 = (rect->x1 + rect->x2)/2;
y0 = (rect->y1 + rect->y2)/2;
return fabs(x-x0)+fabs(y-y0);
}

RectT SelectNearestNodeR(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distRect(mx, my, (RectT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distRect(mx, my, (RectT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[RECT] = mindistance;

return (RectT)NodeObj(list, nearestnode);
}

/*added*/
/*for roundedRectangle*/
void DrawRoundedRect(void *roundedrect)
{
RoundedRectT r = (RoundedRectT)roundedrect;
int pensize = GetPenSize();/*保存當前系統筆畫粗細*/
string color = GetPenColor();/*保存當前系統顏色*/

SetPenSize(r->PenSize);/*設置粗細*/
SetPenColor(r->color);/*設置顏色*/
MovePen(r->x1, r->y1);
DrawLine(r->x2-r->x1, 0);
DrawLine(0, r->y2-r->y1);
DrawLine(r->x1-r->x2, 0);
DrawLine(0, r->y1-r->y2);
SetPenSize(pensize); /*恢復粗細*/
SetPenColor(color);/*恢復顏色*/
/*something to do*/
}

bool RoundedRectEqual(void *roundedrect1, void *roundedrect2)
{
RoundedRectT r1, r2;

r1 = (RoundedRectT)roundedrect1;
r2 = (RoundedRectT)roundedrect2;
return r1->x1 == r2->x1 && r1->y1 == r2->y1 &&
r1->x2 == r2->x2 && r1->y2 == r2->y2;
}

double distRoundedRect(double x, double y, RoundedRectT roundedrect)
{
double x0, y0;

x0 = (roundedrect->x1 + roundedrect->x2)/2;
y0 = (roundedrect->y1 + roundedrect->y2)/2;
return fabs(x-x0)+fabs(y-y0);
}

RoundedRectT SelectNearestNodeRR(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distRoundedRect(mx, my, (RoundedRectT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distRoundedRect(mx, my, (RoundedRectT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[ROUNDEDRECT] = mindistance;

return (RoundedRectT)NodeObj(list, nearestnode);
}

/*added*/
/*for diamond*/
void DrawDiamond (void *diamond)
{
DiamondT d = (DiamondT)diamond;
int pensize = GetPenSize();/*保存當前系統筆畫粗細*/
string color = GetPenColor();/*保存當前系統顏色*/

SetPenSize(d->PenSize);/*設置粗細*/
SetPenColor(d->color);/*設置顏色*/
MovePen(d->x1, (d->y1+d->y2)/2);
DrawLine((d->x2-d->x1)/2, (d->y2-d->y1)/2);
DrawLine((d->x2-d->x1)/2, -(d->y2-d->y1)/2);
DrawLine(-(d->x2-d->x1)/2, -(d->y2-d->y1)/2);
DrawLine(-(d->x2-d->x1)/2, (d->y2-d->y1)/2);
SetPenSize(pensize); /*恢復粗細*/
SetPenColor(color);/*恢復顏色*/
}

bool DiamondEqual(void * diamond1, void * diamond2)
{
DiamondT d1, d2;

d1 = (DiamondT)diamond1;
d2 = (DiamondT)diamond2;
return d1->x1 == d2->x1 && d1->y1 == d2->y1 &&
d1->x2 == d2->x2 && d1->y2 == d2->y2;
}

double distDiamond(double x, double y, DiamondT diamond)
{
double x0, y0;

x0 = (diamond->x1 + diamond->x2)/2;
y0 = (diamond->y1 + diamond->y2)/2;
return fabs(x-x0)+fabs(y-y0);
}

DiamondT SelectNearestNodeD(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distDiamond(mx, my, (DiamondT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distDiamond(mx, my, (DiamondT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[DIAMOND] = mindistance;

return (DiamondT)NodeObj(list, nearestnode);
}

/*For ellipse*/
void DrawCenteredEllipse(void *ellipse)
{
EllipseT eptr = (EllipseT)ellipse;
int pensize = GetPenSize();
string color = GetPenColor();

SetPenSize(eptr->PenSize);
SetPenColor(eptr->color);
MovePen(eptr->cx + eptr->rx, eptr->cy);
DrawEllipticalArc(eptr->rx, eptr->ry, 0.0, 360.0);
SetPenSize(pensize);
SetPenColor(color);
}

bool EllipseEqual(void *ellipse1, void *ellipse2)
{
EllipseT eptr1 = (EllipseT)ellipse1, eptr2 = (EllipseT)ellipse2;

return eptr1->cx == eptr2->cx && eptr1->cy == eptr2->cy &&
eptr1->rx == eptr2->rx && eptr1->ry == eptr2->ry;

}

double distEllipse(double x, double y, EllipseT ellipse)
{
return fabs(x-ellipse->cx) + fabs(y-ellipse->cy);
}

EllipseT SelectNearestNodeE(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distEllipse(mx, my, (EllipseT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distEllipse(mx, my, (EllipseT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[ELLIPSE] = mindistance;

return (EllipseT)NodeObj(list, nearestnode);
}

/*for text*/
void DrawTextT(void *text)
{
TextT tptr = (TextT)text;
string color = GetPenColor();
int pointsize = GetPointSize();

MovePen(tptr->x, tptr->y);
SetPenColor(tptr->color);
SetPointSize(tptr->PointSize);
DrawTextString(tptr->text);
SetPenColor(color);
SetPointSize(pointsize);
}

/*畫光標*/
void DrawCursor(string str, int curPos, double startx, double starty)
{
if (curPos < 0 || curPos > strlen(str)) return;
MovePen(startx+TextStringWidth(SubString(str, 0, curPos-1)), starty);
DrawTextString(CURSOR);
return;
}

bool TextEqual(void *text1, void *text2)
{
TextT tptr1 = (TextT)text1, tptr2 = (TextT)text2;

return StringEqual(tptr1->text, tptr2->text) &&
tptr1->x == tptr2->x && tptr1->y == tptr2->y;
}

double distText(double x, double y, TextT text)
{
return fabs(x - text->x) + fabs(y - text->y);
}

TextT SelectNearestNodeT(linkedlistADT list, double mx, double my)
{
linkedlistADT nearestnode = NULL, ptr;
double mindistance, dist;

ptr = NextNode(list, list);
if (ptr == NULL) return NULL;
nearestnode = ptr;
mindistance = distText(mx, my, (TextT)NodeObj(list, ptr));
while (NextNode(list, ptr) != NULL) {
ptr = NextNode(list, ptr);
dist = distText(mx, my, (TextT)NodeObj(list, ptr));
if (dist < mindistance) {
nearestnode = ptr;
mindistance = dist;
}
}
minDistance[STRING] = mindistance;

return (TextT)NodeObj(list, nearestnode);
}

/*Insert char c to the string str at the position pos*/
void InsertCharToString(string str, int pos, char c)
{
int len;

if (pos < 0 || pos >= TEXTLEN) return;
len = strlen(str);
*(str+len+1) = ‘\0‘;/*插入後形成字符串*/
while (len > pos) {
*(str+len) = *(str+len-1);
len--;
}
*(str+len) = c;
return;
}

/*Delete the character at the pos position of string str*/
void DeleteCharFromString(string str, int pos)
{
int len;

len = strlen(str);
if (pos < 0 || pos >= len) return;

while (*(str+pos) != ‘\0‘) {
*(str+pos) = *(str+pos+1);
pos++;
}
return;
}

/*changed*/
/*for all*/
void PickNearestNode(linkedlistADT list[], double mx, double my)
{
int i, minlist;

curLine = SelectNearestNodeL(list[LINE], mx, my);
curArrowLine=SelectNearestNodeAL(list[ARROWLINE], mx, my);
curRect = SelectNearestNodeR(list[RECT], mx, my);
curRoundedRect=SelectNearestNodeRR(list[ROUNDEDRECT], mx, my);
curDiamond=SelectNearestNodeD(list[DIAMOND], mx, my);
curEllipse = SelectNearestNodeE(list[ELLIPSE], mx, my);
curText = SelectNearestNodeT(list[STRING], mx, my);

minlist = 0;
for (i = 1; i < NLIST; i++) {
if (minDistance[i] < minDistance[minlist]) minlist = i;
}
curList = minlist;
}

void TraverseAllList()
{
TraverseLinkedList(list[LINE], DrawLineD);
TraverseLinkedList(list[ARROWLINE], DrawArrowLineD);
TraverseLinkedList(list[RECT], DrawRect);
TraverseLinkedList(list[ROUNDEDRECT], DrawRoundedRect);
TraverseLinkedList(list[DIAMOND], DrawDiamond);
TraverseLinkedList(list[ELLIPSE], DrawCenteredEllipse);
TraverseLinkedList(list[STRING], DrawTextT);
}http://www.6daixie.com/contents/13/1437.html

因為專業,所以值得信賴。如有需要,請加QQ99515681 或郵箱:[email protected]

微信:codinghelp

代寫C語言程序設計作業、代作國外PickNearestNod C作業