Beispiel #1
0
int is_polygon_self_crossed(
							VertType *vert_ary, short *vert_ind_ary, int poly_num)
{
    int i,j,i1,j1,res;
    double ratio1,ratio2;
    VertType pt;

    for(i=2; i<(poly_num-1); i++)
	{
        i1=(i+1);
        for(j=0; j<=(i-2); j++)
		{
            j1=(j+1)%poly_num;
            res= find_intersection( &vert_ary[vert_ind_ary[i]],
                &vert_ary[vert_ind_ary[i1]],&vert_ary[vert_ind_ary[j]],
				&vert_ary[vert_ind_ary[j1]],&pt,&ratio1,&ratio2);
        
			if(res==1)
			{
				if(!(ratio1>0.9 || ratio1<0.1 ||ratio2>0.9 || ratio2<0.1))
					printf("Warning is_polygon_self_crossed(), ratio1=%lf, ratio2=%lf\n",
                    ratio1,ratio2);
            }
			
			if(res)
				return 1;
        }
    }
    return 0;
}
int main()
{
    std::vector<int> vec1 {1, 2, 3, 3, 4, 4, 5, 5};
    std::vector<int> vec2 {2, 3, 5, 5, 4, 4, 6};
    std::cout << "Vec 1: ";
    print_vector(vec1);
    std::cout << "Vec 2: ";
    print_vector(vec2);
    std::vector<int> result = find_intersection(vec1, vec2);
    std::cout << "Result: ";
    print_vector(result);
    return 0;
}
Beispiel #3
0
int is_line_cross_polygon(int ind0, int ind1,VertType *vert_ary, 
						  short *vert_ind_ary, int poly_num)
						  /* Is this line [ind0, ind1] crosses any other line segment? */
{
    int i,j,res;
    VertType *p0,*p1;
    p0=&vert_ary[vert_ind_ary[ind0]];
    p1=&vert_ary[vert_ind_ary[ind1]];
    
	for(i=0; i<poly_num; i++)
	{
		j=i+1;
		if(j>=poly_num)j=0;
		if(ind0!=i && ind0!=j && ind1!=i && ind1!=j)
		{
			/* not neighboring */
			res=find_intersection(p0,p1,&vert_ary[vert_ind_ary[i]],
				&vert_ary[vert_ind_ary[j]],NULL,NULL,NULL);
			if(res)return 1;
		}
    }
    return 0;
}
Beispiel #4
0
int is_center_line_not_cross_poly(VertType *vt, 
								  VertType *vert_ary, short *vert_ind_ary, int poly_num)
{
    int i,j,j1,res;
    VertType *vt1;
    
	for(i=0; i<poly_num; i++)
	{
        vt1=&vert_ary[i];
        for(j=0; j<poly_num; j++)
		{
            j1=j+1;
            if(j1>=poly_num)j1=0;
            if(i!=j && i!=j1)
			{
                res=find_intersection(vt,vt1,
					&vert_ary[vert_ind_ary[j]],
					&vert_ary[vert_ind_ary[j1]],NULL,NULL,NULL);
                if(res)return 0;
            }
        }
    }
    return 1;
}
Beispiel #5
0
Datei: q2_7.c Projekt: vdloo/SICP
void test_intersects() {
    int i;
    node *ll1, *ll2, *ll3, *tmp;
    ll1 = ll2 = ll3 = NULL;

    declare_linked_list(ll1, "first", 8);
    declare_linked_list(ll2, "second", 12);
    declare_linked_list(ll3, "third", 5);
    tmp->next = ll1->next->next->next->next;

    printf("The third list with intersection looks like: ");
    print_linked_list(ll3);

    printf(
        "Asserting the first linked list does not intersect with the second\n"
    );
    assert(find_intersection(ll1, ll2) == NULL);
    printf(
        "Asserting the second linked list does not intersect with the first\n"
    );
    assert(find_intersection(ll2, ll1) == NULL);
    printf(
        "Asserting the second linked list does not intersect with the third\n"
    );
    assert(find_intersection(ll2, ll3) == NULL);
    printf(
        "Asserting the third linked list does not intersect with the second\n"
    );
    assert(find_intersection(ll3, ll2) == NULL);
    printf(
        "Asserting the first linked list intersects with the third\n"
    );
    assert(find_intersection(ll1, ll3) == tmp->next);
    printf(
        "Asserting the third linked list intersects with the first\n"
    );
    assert(find_intersection(ll1, ll3) == tmp->next);
}
int main()
{
	node_t head = NULL;
	node_t first = NULL;
	node_t second = NULL;
	node_t merged = NULL;
	int choice = 0;
	int ele;
	int pos;
	node_t node_pointer = NULL;
	int n;
	int is_cycle = 0;
	node_t list_cycle = NULL;
	int isPalindrome = 0;
	int intersection = 0;
	node_t resultant_of_addition = NULL;
	int k = 0;
	int m;


	while(1)
	{
		printf("--------------------------------------------------------------------------------------------------------\n");
		printf("1. Add node in the front end \n");
		printf("2. Display the list \n");
		printf("3. Exit \n");
		printf("4. Add node at the rear end of the list \n");
		printf("5. Delete a node at the front end of the list \n");
		printf("6. Delete a node from the rear end of the list \n");
		printf("7. Insert a node in order to the list \n");
		printf("8. Merge two ordered linked lists \n");
		printf("9. Search for an item in the list \n");
		printf("10. Delete a node whose value is specified \n");
		printf("11. Delete a node at the specified position \n");
		printf("12. Reverse  list wihtout creating extra nodes \n");
		printf("13. Delete a node given only a pointer to the node \n");
		printf("14. Print middle element of the list \n");
		printf("15. Print the nth last element in the list \n");
		printf("16. Delete the entire list \n");
		printf("17. Detect a loop in the list \n");
		printf("18. Check whether a list is a palindrome \n");
		printf("19. Find the intersection of two lists \n");
		printf("20. Print reverse recursively \n");
		printf("21. Remove duplicates in a sorted linked list \n");
		printf("22. Move the last node in the list to the first node \n");
		printf("23. Reverse the list pairwise \n");
		printf("24. Find the intersection of two lists recursively \n");
		printf("25. Delete alternate nodes in the list \n");
		printf("26. Alternating split of the list \n");
		printf("27. Delete nodes whose neighbours value is greater \n");
		printf("28. Sepearate into even and odd in that order \n");
		printf("29. Add two lists and give the resultant list \n");
		printf("30. Rotate the list by k elements \n");
		printf("31. Separate into 0s and 1s \n");
		printf("32. Delete n nodes after the first m nodes \n");
		printf("-------------------------------------------------------------------------------------------------------\n");

		printf("Enter the choice\n");
		scanf("%d", &choice);

		switch(choice)
		{
			case 1:

					printf("Enter the element to enter to the front end of the list \n");
					scanf("%d", &ele);
					head = add_front(head, ele);
					break;

			case 2:
					display(head);
					break;

			case 3:
					exit(0);

			case 4:
					printf("Enter an element to be added to the end of the list \n");
					scanf("%d", &ele);
					head = add_end(head, ele);
					break;

			case 5:
					head = delete_front(head);
					break;

			case 6:
					head = delete_rear(head);
					break;

			case 7:
					printf("Enter the element to be inserted \n");
					scanf("%d", &ele);
					head = insert_in_order(head, ele);
					break;

			case 8:

					first = insert_in_order(first, 92);
					first = insert_in_order(first, 42);
					first = insert_in_order(first, 35);

					second = insert_in_order(second, 100);
					second = insert_in_order(second, 432);
					second = insert_in_order(second, 90);
					second = insert_in_order(second, 10);


					printf("The elements of the first list are: \n");
					display(first);

					printf("The elements of the second list are \n");
					display(second);

					merged = merge_ordered_lists(first, second);
					printf("The ordered list is: \n");
					display(merged);

			case 9:
					printf("Enter the element of the list to be searched for \n");
					scanf("%d", &ele);
					int pos = search(head, ele);
					if(pos != -1)
					{
						printf("The element is found at %d: \n", pos);
					}
					else
					{
						printf("The element is not found in the list \n");
					}

					break;

			case 10:
					printf("Enter the element of the list to be deleted: \n");
					scanf("%d", &ele);
					head = delete_with_info(head, ele);

					if(head == (node_t)NULL)
					{
						printf("The list is empty or the element u specified is not found: \n");
					}
					break;

			case 11:
					printf("Enter the position with the first node as position 1 \n");
					scanf("%d", &ele);
					head = delete_with_pos(head, ele);

					if(head == (node_t)NULL)
					{
						printf("Either the list is empty or the position specified is not appropriate \n");
					}

					break;

			case 12:

					head = reverse(head);
					break;

			case 13:
					node_pointer = head -> link -> link;
					delete_node_given_pointer(node_pointer);
					break;

			case 14:
					 print_middle(head);
					 break;

			case 15:
					printf("Enter the value of n \n");
					scanf("%d",&n);
					print_nth_last(head, n);
					break;

			case 16:
					head = delete_list(head);
					break;

			case 17:

					list_cycle = add_end(list_cycle,1);
					list_cycle = add_end(list_cycle,2);
					list_cycle = add_end(list_cycle,3);
					list_cycle = add_end(list_cycle,4);
					list_cycle = add_end(list_cycle,5);
					list_cycle -> link -> link -> link -> link -> link = list_cycle -> link;
					is_cycle = find_cycle(list_cycle);
					if(is_cycle)
					{
						printf("There is a cycle in the list \n");

					}

					else
					{
						printf("There is no cycle in the list \n");
					}



					break;

			case 18:

					isPalindrome = is_palindrome(&head, head);
					if(isPalindrome)
					{
						printf("The list is a palindrome \n");
					}

					else
					{
						printf("The list is not a palindrome \n");
					}
					break;

			case 19:

					first = add_end(first,10);
					first = add_end(first,20);
					


					second = add_end(second,43);
					second = add_end(second,3);
					second = add_end(second,34);
					second = add_end(second,44);

					first -> link -> link = second -> link;


					intersection = find_intersection(first, second);
					printf("The intersection point of the two lists are %d \n", intersection);
					break;

			case 20:
					print_reverse_recursively(head);
					printf("\n");
					break;

			case 21:

					remove_duplicates(head);
					break;

			case 22:

					head = move_last_to_first(head);
					break;

			case 23:

					head = pairwise_reverse(head);
					break;

			case 24:

					first = add_end(first, 10);
					first = add_end(first, 30);
					first = add_end(first, 40);
					first = add_end(first, 50);
					first = add_end(first, 60);


					second = add_end(second, 10);
					second = add_end(second, 20);
					second = add_end(second, 30);


					find_common_recursively(first, second);
					break;

			case 25:
					head = delete_alternate(head);
					break;

			case 26:
					 alternating_split_v2(head);
					 break;

			case 27:
					head = delete_node_when_neigbour_higher(head);
					break;

			case 28:
					head = separate_into_even_odd_v2(head);
					break;

			case 29:
						first = add_front(first, 2);
						first = add_front(first, 4);
						first = add_front(first, 8);


						second = add_front(second,2);
						second = add_front(second,4);
						second = add_front(second,5);
						second = add_front(second,3);

						resultant_of_addition = add_two_lists(first, second);

						printf("The resultant list is as follows \n");
						display(resultant_of_addition);

						break;	

			case 30:
					printf("Enter the value of k \n");
					scanf("%d",&k);
					head = rotate_by_k(head, k);
					break;

			case 31:
					head = separate_into_zeroes_ones(head);
					break;

			case 32:
					printf("Enter the value of m \n");
					scanf("%d", &m);
					printf("Enter the value of n \n");
					scanf("%d", &n);
					head = retain_m_delte_n(head, m , n);
					break;



			default:
					printf("Invalid choice... Please try again \n");
					break;

		}

	}
}
void draw_world()
{
    world_z = world.height+25;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -world_z);
    int i,j,k;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    int c = 0;
    draw_box(world.width,world.height);
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_projectors; i++)
    {
        if(select_type == PROJECTOR && select_number == i)
            draw_selected_line(projector[i].l);
        else
            draw_line(projector[i].l);
        draw_point(projector[i].d);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    c++;
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_mirrors; i++)
    {
        if(select_type == MIRROR && select_number == i)
            draw_selected_line(mirror[i].l);
        else
            draw_line(mirror[i].l);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    c++;
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_blocks; i++)
    {
        if(select_type == BLOCK && select_number == i)
            draw_selected_line(block[i].l);
        else
            draw_line(block[i].l);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    /*for(i=0; i<num_projectors; i++)
    {
        for(j=0; j<projector[i].num_pixels; j++)
            draw_line(projector[i].pixels[j]);
        draw_point(projector[i].d);
    }*/
    for(i=0; i<num_projectors; i++)
    {
        for(j=0; j<projector[i].num_pixels; j++)
        {
            glColor3f(color[c].r,color[c].g,color[c++].b);
            point p,fp;
            float dist = INF;
            int type = 1,num;
            for(k=0; k<num_mirrors; k++)
            {
                p = find_intersection(mirror[k].l,projector[i].pixels[j]);
                if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) )
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 1;
                            num = k;
                            if(find_side(mirror[k].l.p1,mirror[k].l.p2,projector[i].pixels[j].p1) == -1)
                                type = 2;
                        }
                    }
            }
            for(k=0; k<num_blocks; k++)
            {
                p = find_intersection(block[k].l,projector[i].pixels[j]);
                if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p))
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 2;
                        }
                    }
            }
            for(k=0; k<num_world; k++)
            {
                p = find_intersection(world.l[k],projector[i].pixels[j]);
                if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p))
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 2;
                        }
                    }
            }
            for(k=0; k<num_projectors; k++)
            {
                if(k!=i)
                {
                    p = find_intersection(projector[k].l,projector[i].pixels[j]);
                    if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p))
                        if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                        {
                            float d = distance_between_points(p,projector[i].pixels[j].p1);
                            if( d < dist )
                            {
                                fp = p;
                                dist = d;
                                type = 2;
                            }
                        }
                }
            }
            draw_line2(fp,projector[i].pixels[j].p2);
            if(type==1)
            {
                //float angle = find_angle(projector[i].pixels[j],mirror[num].l);
                //float angle = find_angle2(projector[i].pixels[j].p1,fp,mirror[num].l.p1,mirror[num].l.p2);
                line n;
                n.p1 = fp;
                n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(projector[i].pixels[j].m));
                n.c = n.p1.y - n.p1.x*n.m;
                draw_reflections(n,num,1);
            }
        }
    }
    //glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();
    //glPushMatrix();
    //glTranslatef(0.0f, 0.0f, -5.0f);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    //glRotatef(rot,1,2,0);
    //glRasterPos2f(-0.5,0);
    //glColor4f(0.0f, 1.0f, 1.0f,1.0f);
    //glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, "Rishi Raj Singh Jhelumi");
    //glBegin(GL_POINTS);
    //glVertex3f(0,0,-world_z);
    //glEnd();
    //glPopMatrix();
    glFlush();
    //glutSwapBuffers();
}
void draw_reflections(line l,int m,int side)
{
    int i,j,k;
    point p,fp;
    float dist = INF;
    int type = 1,num;
    for(k=0; k<num_mirrors; k++)
    {
        if(k!=m)
        {
            p = find_intersection(mirror[k].l,l);
            if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) )
                if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
                {
                    float d = distance_between_points(p,l.p1);
                    if( d < dist )
                    {
                        fp = p;
                        dist = d;
                        type = 1;
                        num = k;
                        if(find_side(mirror[k].l.p1,mirror[k].l.p2,l.p1) == -1)
                            type = 2;
                    }
                }
        }
    }
    for(k=0; k<num_blocks; k++)
    {
        p = find_intersection(block[k].l,l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    for(k=0; k<num_world; k++)
    {
        p = find_intersection(world.l[k],l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    for(k=0; k<num_projectors; k++)
    {
        p = find_intersection(projector[k].l,l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    draw_line2(fp,l.p1);
    if(type==1)
    {
        //float angle = find_angle(l,mirror[m].l);
        line n;
        n.p1 = fp;
        n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(l.m));
        n.c = n.p1.y - n.p1.x*n.m;
        draw_reflections(n,num,1);
    }
}
TEST_F(IntersectionsTest, FindLinesIntersectionTest) {
  {
    const point p0{1, 4}, p1{5, 6};
    const point q0{4, 1}, q1{2, 9};
    const point i{3, 5};

    SCOPED_TRACE("First test");
    check_equal(i, cpl::find_lines_intersection(p0, p1, q0, q1));
    check_equal(i, cpl::find_lines_intersection(p0, p1, q1, q0));
    check_equal(i, cpl::find_lines_intersection(p1, p0, q0, q1));
    check_equal(i, cpl::find_lines_intersection(p1, p0, q1, q0));
  }

  const auto a = line{{-3, 1}, {4, 1}};
  const auto b = line{{-1, -2}, {-1, 5}};
  const auto c = line{{4, 4}, {1, 3}};
  const auto d = line{{-2, 5}, {3, 0}};

  check_equal(point(-1, 1), find_intersection(a, b));
  check_equal(point(-5, 1), find_intersection(a, c));
  check_equal(point(2, 1), find_intersection(a, d));

  check_equal(point(-1, 1), find_intersection(b, a));
  check_equal(point(-1, 7.0f / 3), find_intersection(b, c));
  check_equal(point(-1, 4), find_intersection(b, d));

  check_equal(point(-5, 1), find_intersection(c, a));
  check_equal(point(-1, 7.0f / 3), find_intersection(c, b));
  check_equal(point(0.25f, 2.75f), find_intersection(c, d));

  check_equal(point(2, 1), find_intersection(d, a));
  check_equal(point(-1, 4), find_intersection(d, b));
  check_equal(point(0.25f, 2.75f), find_intersection(d, c));
}
Beispiel #10
0
uint32_t naivepsi(role_type role, uint32_t neles, uint32_t pneles, task_ctx ectx,
		crypto* crypt_env, CSocket* sock, uint32_t ntasks, uint32_t* matches) {

	uint32_t i, intersect_size, maskbytelen;
	//task_ctx_naive ectx;
	CSocket* tmpsock = sock;

	uint32_t* perm;
	uint8_t *hashes, *phashes;

	maskbytelen = ceil_divide(crypt_env->get_seclvl().statbits + ceil_log2(neles) + ceil_log2(pneles), 8);

	hashes = (uint8_t*) malloc(sizeof(uint8_t) * neles * maskbytelen);
	perm  = (uint32_t*) malloc(sizeof(uint32_t) * neles);


	/* Generate the random permutation the elements */
	crypt_env->gen_rnd_perm(perm, neles);

	/* Hash and permute elements */
#ifdef DEBUG
	cout << "Hashing my elements" << endl;
#endif

	//ectx.eles.input = permeles;
	//ectx.eles.inbytelen = elebytelen;
	ectx.eles.outbytelen = maskbytelen,
	ectx.eles.nelements = neles;
	ectx.eles.output = hashes;
	ectx.eles.perm = perm;
	ectx.sctx.symcrypt = crypt_env;

	run_task(ntasks, ectx, psi_hashing_function);

	phashes = (uint8_t*) malloc(sizeof(uint8_t) * pneles * maskbytelen);


#ifdef DEBUG
	cout << "Exchanging hashes" << endl;
#endif
	snd_and_rcv(hashes, neles * maskbytelen, phashes, pneles * maskbytelen, tmpsock);

	/*cout << "Hashes of my elements: " << endl;
	for(i = 0; i < neles; i++) {
		for(uint32_t j = 0; j < maskbytelen; j++) {
			cout << (hex) << (uint32_t) hashes[i * maskbytelen + j] << (dec);
		}
		cout << endl;
	}

	cout << "Hashes of partner elements: " << endl;
	for(i = 0; i < pneles; i++) {
		for(uint32_t j = 0; j < maskbytelen; j++) {
			cout << (hex) << (uint32_t) phashes[i * maskbytelen + j] << (dec);
		}
		cout << endl;
	}*/
#ifdef DEBUG
	cout << "Finding intersection" << endl;
#endif
	intersect_size = find_intersection(hashes, neles, phashes, pneles, maskbytelen,
			perm, matches);


#ifdef DEBUG
	cout << "Free-ing allocated memory" << endl;
#endif
	free(perm);
	free(hashes);
	//free(permeles);
	free(phashes);

	return intersect_size;
}
int main()
{
	int choice;
	int array[MAX];
	int second_array[MAX];
	int n;
	int n1;
	int odd_occurences;
	int i = 0;
	int max_difference;

	struct pair min_max;
	int x;
	int isMajority;
	int maxSumContiguous;
	int missing;
	int searchElement;
	int searchIndex;
	int rotateBy;
	int twod[3][3];
	int j = 0;

	int sorted_array[] = {-8,1,4,6,10,45};
	int target_sum = 16;

	while(1)
	{
		printf("------------------------------------------------------------------------------------------------------\n");
		printf("1. Find the element occuring odd number of times in the array \n"); 
		printf("2. Exit \n"); 
		printf("3. Find the union of two arrays \n"); 
		printf("4. Find the intersection of two sorted arrays \n"); 
		printf("5. Find the maximum difference between two elements in an array \n");
		printf("6. Separate into zeroes and ones in a binary array of one dimentsion \n");
		printf("7. Find the minimum and maximum elements of the array \n");
		printf("8. Check for majority element in the array \n"); 
		printf("9. Find the first and the second smallest element in an array \n"); 
		printf("10. Find all the elements in the araay which have all the elements on the right which are lesser than the element \n"); 
		printf("11. Largest sum contiguous sub array \n");
		printf("12. Find the missing number among n contiguous elements in an array \n"); 
		printf("13. Find an element in a rotated sorted array(find element in this array 4567123) \n"); 
		printf("14. Reverse the array \n");
		printf("15. Left rotate an array \n"); 
		printf("16. If an element in a matrix make the corresponding row and column as zero \n");
		printf("17. Given a sorted array and a target integer find two elements of the array whose sum equals the target integer \n");
		printf("18. Right rotate an array \n"); 
		printf("------------------------------------------------------------------------------------------------------\n");

		printf("Enter the choice \n");
		scanf("%d", &choice);

		switch(choice)
		{
			case 1:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);

					printf("Enter the elements of the array \n");
					for(i = 0 ; i < n ; i++)
					{
						scanf("%d", &array[i]);
					}
					odd_occurences =  find_odd_occurence(array,n);
					printf("The element occuring odd number of times is %d \n", odd_occurences);
					break;

			case 2:
					exit(1);

			case 3:
					printf("Enter the number of elements of the array1 \n");
					scanf("%d",&n);

					printf("Enter the elements of the array 1 \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}


					printf("Enter the number of elements of the array2 \n");
					scanf("%d", &n1);

					printf("Enter the elements of the array 2 \n");
					for(i = 0; i < n1; i++)
					{
						scanf("%d",&second_array[i]);
					}

					find_union(array,n,second_array,n1);
					break;

			case 4:
					printf("Enter the number of elements of the array1 \n");
					scanf("%d",&n);

					printf("Enter the elements of the array 1 \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}


					printf("Enter the number of elements of the array2 \n");
					scanf("%d", &n1);

					printf("Enter the elements of the array 2 \n");
					for(i = 0; i < n1; i++)
					{
						scanf("%d",&second_array[i]);
					}

					find_intersection(array,n,second_array,n1);
					break;

			case 5:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);
					printf("Enter the elements of the array \n");

					for(i = 0 ; i < n; i++)
					{
						scanf("%d", &array[i]);
					}

					max_difference = find_max_difference(array,n);
					printf("The maximum difference in the array %d \n", max_difference);
					break;

			case 6:
					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);
					printf("Enter the elements of the array \n");

					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					separate_into_zeroes_ones(array,n);
					display(array,n);
					break;

			case 7:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);
					printf("Enter the elements of the array \n");

					for(i = 0 ; i < n; i++)
					{
						scanf("%d", &array[i]);
					}

					min_max =  find_min_max(array, n);
					printf("The maximum element of the array is %d \n",min_max.max);
					printf("The minimum element of the array is %d \n", min_max.min);
					break;

			case 8:
					printf("Enter the number of elements in the array \n");
					scanf("%d", &n);
					printf("Enter the element to be searched for \n");
					scanf("%d", &x);

					printf("Enter the elements of the array \n");

					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					isMajority = is_majority(array,n,x);

					if(isMajority)
					{
						printf("The element you entered is a majority element \n");

					}

					else
					{
						printf("The element you enetered is not a majority element \n");
					}

					break;

			case 9:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);

					printf("Enter the elements of the array one by one \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d", &array[i]);
					}

					find_first_second_smallest(array,n);
					break;

			case 10:
					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);

					printf("Enter the elements of the array \n");
					for(i = 0; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					find_leader(array,n);

					break;

			case 11:

					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);
					printf("Enter the elements of the array \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					maxSumContiguous = find_largest_sum_contiguous(array,n);
					printf("The maximum sum is %d \n",maxSumContiguous);
					break;

			case 12:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);
					printf("Enter the elements of the array \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					missing = find_missing_number(array,n);
					printf("The missing number is %d \n", missing);


					break;

			case 13:
					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);

					printf("Enter the elements of the array \n");
					for(i = 0 ; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					printf("Enter the search element \n");
					scanf("%d",&searchElement);

					searchIndex = find_element_in_rotated(array,n,searchElement);
					if(searchIndex != -1)
					{
						printf("The element is found in the array at %d \n",searchIndex);
					}

					else
					{
						printf("The element is not found in the array \n");
					}

					break;

			case 14:
					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);
					
					printf("Enter the elements of the array \n");
					for(i = 0; i < n; i++)
					{
						scanf("%d",&array[i]);
					}

					reverse(array,0,n-1);
					display(array,n);
					break;

			case 15:
					printf("Enter the number of elements of the array \n");
					scanf("%d",&n);
					printf("Enter the elements of the array \n");
					for(i = 0; i <n; i++)
					{
						scanf("%d",&array[i]);
					}

					printf("Enter the number of elements by which the array has to be rotated \n");
					scanf("%d",&rotateBy);
					rotate(array,rotateBy,n);
					display(array,n);
					break;


			case 16:
					
					printf("Enter the elements of the array \n");
					for(i = 0; i < 3; i++)
					{
						for(j = 0; j < 3; j++)
						{
							scanf("%d",&twod[i][j]);
						}
					}

					printf("This is from main \n");
					for(i = 0; i < 3; i++)
					{
						for(j = 0; j < 3; j++)
						{
							printf("%d \t",twod[i][j]);
						}

						printf("\n");
					}

					make0rows0columns(twod);

					break;

			case 17:

					findTwoElementsTarget(sorted_array,target_sum,6);
					break;

			case 18:
					printf("Enter the number of elements of the array \n");
					scanf("%d", &n);
					printf("Enter the elements of the array \n");
					for(i = 0; i <n; i++)
					{
						scanf("%d",&array[i]);
					}

					printf("Enter the number of elements by which the array has to be rotated \n");
					scanf("%d",&rotateBy);
					right_rotate(array,rotateBy, n);
					display(array,n);
					break;
			default:
					break;




		}

	}
}
Beispiel #12
0
void draw_contours(double **fxy, double xmin, double xmax, double ymin, double ymax,
                   long nx, long ny, double *cval, long nc)
{
    double x[2], y[2], f[2][2], xx[4], yy[4];
    long ix, iy, ic;
    double fxymax, fxymin, value;
    double dx, dy;
    long nn;

    dx = (xmax-xmin)/(nx-1);
    dy = (ymax-ymin)/(ny-1);

    fxymin = fxymax = fxy[0][0];
    for (ix=0; ix<nx; ix++) {
        for (iy=0; iy<ny; iy++) {
            if ((value = fxy[ix][iy])>fxymax)
                fxymax = value;
            if (value<fxymin)
                fxymin = value;
            }
        }

    /* eliminate meaningless contours */
    for (ic=0; ic<nc; ic++) {
        if (cval[ic]<fxymin) {
            cval += 1;
            nc--;
            ic--;
            }
        else if (cval[ic]>fxymax) {
            nc = ic;
            break;
            }
        }

    /* sweep over region of plot and draw contours */
    for (ix=0; ix<nx-1; ix++) {
        x[1] = (x[0] = ix+1) + 1;
        for (iy=0; iy<ny-1; iy++) {
            y[1] = (y[0] = iy+1) + 1;
            f[0][0] = fxy[ix  ][iy  ];
            f[0][1] = fxy[ix  ][iy+1];
            f[1][0] = fxy[ix+1][iy  ];
            f[1][1] = fxy[ix+1][iy+1];
            for (ic=0; ic<nc; ic++) {
                nn = 0;
                if ((f[0][0]<=cval[ic] && f[1][0]>=cval[ic]) || 
                    (f[1][0]<=cval[ic] && f[0][0]>=cval[ic])    )
                    find_intersection(x[0],y[0],f[0][0],f[1][0],cval[ic],xx,yy,&nn);
                if ((f[0][0]<cval[ic] && f[0][1]>=cval[ic]) || 
                    (f[0][1]<=cval[ic] && f[0][0]>cval[ic])    )
                    find_intersection(y[0],x[0],f[0][0],f[0][1],cval[ic],yy,xx,&nn);
                if ((f[0][1]<=cval[ic] && f[1][1]>=cval[ic]) || 
                    (f[1][1]<=cval[ic] && f[0][1]>=cval[ic])    )
                    find_intersection(x[0],y[1],f[0][1],f[1][1],cval[ic],xx,yy,&nn);
                if ((f[1][0]<=cval[ic] && f[1][1]>cval[ic]) || 
                    (f[1][1]<cval[ic] && f[1][0]>=cval[ic])    )
                    find_intersection(y[0],x[1],f[1][0],f[1][1],cval[ic],yy,xx,&nn);
                while (nn>=2) {
                    nn -= 2;
                    xx[nn]   = (xx[nn  ]-1)*dx + xmin;
                    xx[nn+1] = (xx[nn+1]-1)*dx + xmin;
                    yy[nn]   = (yy[nn  ]-1)*dy + ymin;
                    yy[nn+1] = (yy[nn+1]-1)*dy + ymin;
                    plot_lines(xx+nn, yy+nn, 2, PRESET_LINETYPE,0);
                    }
                }
            }
        }
    }
Beispiel #13
0
Linklist *record_break_contour(int topdown, int group, int ind0)
{
#define BREAK_ARY_SIZE 100
    extern int NUMERICALLY_STABLE; 
    int ind1,topdown1;
    int j,j1,k,k1,i,i1,cnt=0,kkk;
    int group1;
    double ratio_ary[BREAK_ARY_SIZE],pre_ratio;
    double ratio_ary1[BREAK_ARY_SIZE];
    Linklist *linklist;
	
    topdown1=1-topdown;
    ind1=ind0+1;
    if(ind1>=New_group_ind_ary[topdown][group])ind1=0;
    j =New_group_ary[topdown][group][ind0];
    j1=New_group_ary[topdown][group][ind1];

    for(group1=0; group1<Group_num[topdown1]; group1++)
	{
		if(topdown)	kkk=group*Group_num[0]+group1;
		else	 kkk=group1*Group_num[0]+group;
		
		/*
		if(Group_connection_table[kkk]){
		*/

		for(i=0; i<New_group_ind_ary[topdown1][group1]; i++)
		{
			int res;
			i1=i+1;
			if(i1>=New_group_ind_ary[topdown1][group1])i1=0;
			k=New_group_ary[topdown1][group1][i];
			k1=New_group_ary[topdown1][group1][i1];
			res=find_intersection(&(Lines_ary[topdown][j]),
				&(Lines_ary[topdown][j1]), &(Lines_ary[topdown1][k]),
				&(Lines_ary[topdown1][k1]), NULL, &(ratio_ary[cnt]),NULL);
		
			if(res==1 || res==2 || res==5)
			{ 
				cnt++; 
				if(cnt>=BREAK_ARY_SIZE)
				{
					fprintf(stderr,"over BREAK_ARY_SIZE\n");
					exit(1);
				}
			}
		}
		/*
		}
		*/
    }
    if(!cnt)return NULL;
    bubble_sort(ratio_ary,cnt); /* bubble sort is slow, but N is small */
								/* the break point is between the intersection point, and the intersection
	points themselves */
    /* get rid of the duplicated point */
    j=0;
    i=0;

    while(i<cnt && (ratio_ary[i]<=0.0001 || ratio_ary[i]>=0.9999) )i++;
    
	if(i==cnt)return NULL;
    
	pre_ratio=ratio_ary[i];
    ratio_ary1[j++]=pre_ratio;
    i++;
    
	for(; i<cnt; i++)
	{
	/*	if(ratio_ary[i]>(pre_ratio+0.001) && ratio_ary[i] < 0.999) { 
	if(ratio_ary[i] < 0.9999) { 
		*/
		/* different point */
		ratio_ary1[j++]=ratio_ary[i];
		pre_ratio=ratio_ary[i];
		/*
		}
		*/
    }
    cnt=j;
    if(!cnt)return NULL;
	
    if(NUMERICALLY_STABLE)
	{
		if(cnt<=1)return NULL;
		j=0;
		
		for(i=1; i<cnt; i++)
		{
			ratio_ary1[j++]=(ratio_ary1[i-1]+ratio_ary1[i])/2.0;
		}
		
		cnt=j;
    }
	
    linklist=(Linklist*)mycalloc(sizeof(Linklist));
    linklist->index=ind0;
    linklist->ary_ind=cnt;
    linklist->ratio_ary=(double*)mycalloc(sizeof(double)*cnt);
    
	for(i=0; i<cnt; i++)	linklist->ratio_ary[i]=ratio_ary1[i];
    
	linklist->next=NULL;
    
	return linklist;
}
Beispiel #14
0
int is_line_legal(int mode, VertType *p0, VertType *p1, 
				  TileType *tile0, TileType *tile1)
				  /* p0 is on top slice */
				  /* if mode==1, then check the tiling crossing only */
{
    TileType *tile2;
    int i,j,k,debug;
    int in_LS[2];
    if(mode)goto check_tile_only;
	/*
    if(debug_check(p0,131.0, 124.798) && debug_check(p1, 134.0, 122.906))
	debug_pt();
	*/
    /* return 0,1,2: for contour, LS, RS */
    in_LS[0]=is_pt_in_LS(p1, tile0);
    
	if(tile0->nec_polarity==0)
	{ 
		/* negative nec, should be not in RS */
		if(in_LS[0]==2)return 0;
    }
    else if(tile0->nec_polarity==1)
	{
		/* positive nec, should not be in LS */
		if(in_LS[0]==1)return 0;
    }
    else if(tile0->nec_polarity==2)
	{
		/* overlapping pt */
        int group,ind; /* find its overlapping point */
        group=tile0->group;
        ind=tile0->ind;
        
		if(group>=0) 
		{
            tile2=&(Best_tiling_table[1][group][ind]);
			in_LS[1]=is_pt_in_LS(p1, tile2);
			if(in_LS[0]==in_LS[1] && in_LS[0])return 0; /* both on RS, or LS */
		}
		else printf("***Error is_line_legal(0) group=%d\n"); 
    }
	
    in_LS[0]=is_pt_in_LS(p0, tile1);

    if(tile1->nec_polarity==0)
	{
		/* negative nec, should be not in RS */
		if(in_LS[0]==2)return 0;
    }
    else if(tile1->nec_polarity==1)
	{
		/* positive nec, should not be in LS */
		if(in_LS[0]==1)return 0;
    }
    else if(tile1->nec_polarity==2)
	{
		/* overlapping pt */
        int group,ind; /* find its overlapping point */
        group=tile1->group;
        ind=tile1->ind;
        
		if(group>=0) 
		{
            tile2=&(Best_tiling_table[0][group][ind]);
			in_LS[1]=is_pt_in_LS(p0, tile2);
			if(in_LS[0]==in_LS[1] && in_LS[0])return 0; /* both on RS, or LS */
		}
		else printf("***Error is_line_legal(1) group=%d\n"); 
    }
	
	
    for(i=0; i<2; i++)
	{
		short **group_ary,*group_ind_ary;
		VertType *lines_ary;
		group_ary=New_group_ary[i];
		group_ind_ary=New_group_ind_ary[i];
		lines_ary=Lines_ary[i];
		
		for(j=0; j<Group_num[i]; j++)
		{
			for(k=0; k<group_ind_ary[j]; k++)
			{
				int k1,j0,j1;
				k1=k+1;
			
				if(k1>=group_ind_ary[j])k1=0;
				j0=group_ary[j][k];
				j1=group_ary[j][k1];
				
				if(find_intersection(p0, p1, &(lines_ary[j0]),
					&(lines_ary[j1]),NULL,NULL,NULL)==1)return 0;
			}
		}
    }
check_tile_only:;

		debug=0;
		/*
		if(Untiled_ary_index[0] || Untiled_ary_index[1]){
		debug_check(p0,p1);
		debug=1;
		}
		*/
		for(mode=0; mode<2; mode++)
		{
			BoundaryType *temp;
			BoundaryType **untiled_ary;
			untiled_ary=Untiled_ary[mode];
			for(i=0; i<Untiled_ary_index[mode]; i++)
			{
				temp=untiled_ary[i];
				if(temp)
				{
					/* not empty */
					int res;
					VertType *p2,*p3;
					short group0,group1,ind0,ind1;
					group0=temp->group0;
					group1=temp->group1;
					ind0=temp->ind0;
					ind1=temp->ind1;
					p2=&Lines_ary[0][New_group_ary[0][group0][ind0]];
					p3=&Lines_ary[1][New_group_ary[1][group1][ind1]];
					/*
					if((int)(*p0)[0]==114 && (int)(*p0)[1]==103 &&
					(int)(*p1)[0]==117 && (int)(*p1)[1]==102 &&
					(int)(*p2)[0]==118 && (int)(*p2)[1]==103 &&
					(int)(*p3)[0]==116 && (int)(*p3)[1]==101) 
					debug_pt();
					if((int)(*p2)[0]==114 && (int)(*p2)[1]==103 &&
					(int)(*p3)[0]==117 && (int)(*p3)[1]==102 &&
					(int)(*p0)[0]==118 && (int)(*p0)[1]==103 &&
					(int)(*p1)[0]==116 && (int)(*p1)[1]==101) 
					debug_pt();
					*/
					res=find_intersection(p0, p1, p2, p3, NULL,NULL,NULL);
					
					if(res==1 || res==2) return 0; /* intersection */
					
					if(res==5)
					{ /* overlapping, check it in 3D */
						res=find_intersection_3D(p0, p1, p2, p3, NULL,NULL);
						if(res==1)return 0; /* 3 is overlapping line */
					}
					/* else if(res==3){}  intersect at both end points */
				}
			}
		}
				return 1;
}